Skip to content

Commit f4facfa

Browse files
author
hongyangAndroid
committed
增加autolayout-widget模块,目前包含:AutoTabLayout,AutoCardView
1 parent 546aea3 commit f4facfa

File tree

22 files changed

+405
-95
lines changed

22 files changed

+405
-95
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*.apk
66
*.ap_
77

8+
9+
810
# Generated files
911
bin/
1012
gen/

AutoLayoutDemoForEclipse.zip

-4.3 MB
Binary file not shown.

autolayout-widget/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

autolayout-widget/build.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.1"
6+
7+
defaultConfig {
8+
minSdkVersion 10
9+
targetSdkVersion 23
10+
versionCode 1
11+
versionName "1.0"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
compile fileTree(dir: 'libs', include: ['*.jar'])
23+
compile 'com.android.support:appcompat-v7:23.1.0'
24+
compile project(':autolayout')
25+
compile 'com.android.support:support-v4:23.1.0'
26+
compile 'com.android.support:design:23.1.0'
27+
compile 'com.android.support:gridlayout-v7:23.1.0'
28+
compile 'com.android.support:cardview-v7:23.1.0'
29+
}

autolayout-widget/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.zhy.autolayout.widget;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application>
10+
{
11+
public ApplicationTest()
12+
{
13+
super(Application.class);
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.zhy.autolayout.widget">
3+
4+
<application android:allowBackup="true"
5+
android:label="@string/app_name"
6+
>
7+
8+
</application>
9+
10+
</manifest>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.zhy.autolayout.widget;
2+
3+
import android.content.Context;
4+
import android.support.v7.widget.CardView;
5+
import android.util.AttributeSet;
6+
7+
import com.zhy.autolayout.AutoFrameLayout;
8+
import com.zhy.autolayout.utils.AutoLayoutHelper;
9+
10+
/**
11+
* Created by zhy on 15/12/8.
12+
*/
13+
public class AutoCardView extends CardView
14+
{
15+
private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);
16+
17+
public AutoCardView(Context context)
18+
{
19+
super(context);
20+
}
21+
22+
public AutoCardView(Context context, AttributeSet attrs)
23+
{
24+
super(context, attrs);
25+
}
26+
27+
public AutoCardView(Context context, AttributeSet attrs, int defStyleAttr)
28+
{
29+
super(context, attrs, defStyleAttr);
30+
}
31+
32+
@Override
33+
public AutoFrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
34+
{
35+
return new AutoFrameLayout.LayoutParams(getContext(), attrs);
36+
}
37+
38+
@Override
39+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
40+
{
41+
if (!isInEditMode())
42+
{
43+
mHelper.adjustChildren();
44+
}
45+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
46+
}
47+
48+
49+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.zhy.autolayout.widget;
2+
3+
import android.content.Context;
4+
import android.content.res.TypedArray;
5+
import android.support.design.widget.TabLayout;
6+
import android.util.AttributeSet;
7+
import android.util.TypedValue;
8+
import android.view.ViewGroup;
9+
import android.widget.TextView;
10+
11+
import com.zhy.autolayout.utils.AutoUtils;
12+
import com.zhy.autolayout.utils.DimenUtils;
13+
14+
/**
15+
* Created by zhy on 16/3/3.
16+
*/
17+
public class AutoTabLayout extends TabLayout
18+
{
19+
private static final int NO_VALID = -1;
20+
private int mTextSize;
21+
private boolean mTextSizeBaseWidth = false;
22+
23+
public AutoTabLayout(Context context)
24+
{
25+
this(context, null);
26+
}
27+
28+
public AutoTabLayout(Context context, AttributeSet attrs)
29+
{
30+
this(context, attrs, 0);
31+
}
32+
33+
public AutoTabLayout(Context context, AttributeSet attrs, int defStyleAttr)
34+
{
35+
super(context, attrs, defStyleAttr);
36+
37+
initTextSizeBaseWidth(context, attrs);
38+
39+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabLayout,
40+
defStyleAttr, R.style.Widget_Design_TabLayout);
41+
int tabTextAppearance = a.getResourceId(R.styleable.TabLayout_tabTextAppearance,
42+
R.style.TextAppearance_Design_Tab);
43+
44+
mTextSize = loadTextSizeFromTextAppearance(tabTextAppearance);
45+
a.recycle();
46+
}
47+
48+
private void initTextSizeBaseWidth(Context context, AttributeSet attrs)
49+
{
50+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoTabLayout);
51+
mTextSizeBaseWidth = a.getBoolean(R.styleable.AutoTabLayout_auto_textSize_base_width, false);
52+
a.recycle();
53+
}
54+
55+
private int loadTextSizeFromTextAppearance(int textAppearanceResId)
56+
{
57+
TypedArray a = getContext().obtainStyledAttributes(textAppearanceResId,
58+
R.styleable.TextAppearance);
59+
60+
try
61+
{
62+
if (!DimenUtils.isPxVal(a.peekValue(R.styleable.TextAppearance_android_textSize)))
63+
return NO_VALID;
64+
return a.getDimensionPixelSize(R.styleable.TextAppearance_android_textSize, NO_VALID);
65+
} finally
66+
{
67+
a.recycle();
68+
}
69+
}
70+
71+
@Override
72+
public void addTab(Tab tab, int position, boolean setSelected)
73+
{
74+
super.addTab(tab, position, setSelected);
75+
setUpTabTextSize(tab);
76+
}
77+
78+
@Override
79+
public void addTab(Tab tab, boolean setSelected)
80+
{
81+
super.addTab(tab, setSelected);
82+
setUpTabTextSize(tab);
83+
}
84+
85+
private void setUpTabTextSize(Tab tab)
86+
{
87+
if (mTextSize == NO_VALID || tab.getCustomView() != null) return;
88+
89+
ViewGroup tabGroup = (ViewGroup) getChildAt(0);
90+
ViewGroup tabContainer = (ViewGroup) tabGroup.getChildAt(tab.getPosition());
91+
TextView textView = (TextView) tabContainer.getChildAt(1);
92+
93+
if (AutoUtils.autoed(textView))
94+
{
95+
return;
96+
}
97+
if (mTextSizeBaseWidth)
98+
{
99+
mTextSize = AutoUtils.getPercentWidthSize(mTextSize);
100+
} else
101+
{
102+
mTextSize = AutoUtils.getPercentHeightSize(mTextSize);
103+
}
104+
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
105+
}
106+
107+
108+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<declare-styleable name="AutoTabLayout">
4+
<attr name="auto_textSize_base_width" format="boolean"></attr>
5+
</declare-styleable>
6+
</resources>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">autolayout-widget</string>
3+
</resources>

autolayout/src/main/java/AutoContentFrameLayout.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
import android.support.v7.internal.widget.ContentFrameLayout;
33
import android.util.AttributeSet;
44

5+
import com.zhy.autolayout.AutoFrameLayout;
6+
import com.zhy.autolayout.utils.AutoLayoutHelper;
7+
58
/**
69
* Created by zhy on 16/2/25.
710
*/
811
public class AutoContentFrameLayout extends ContentFrameLayout
912
{
13+
private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);
14+
15+
1016
public AutoContentFrameLayout(Context context)
1117
{
1218
super(context);
@@ -21,4 +27,20 @@ public AutoContentFrameLayout(Context context, AttributeSet attrs, int defStyleA
2127
{
2228
super(context, attrs, defStyleAttr);
2329
}
30+
31+
@Override
32+
public AutoFrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
33+
{
34+
return new AutoFrameLayout.LayoutParams(getContext(), attrs);
35+
}
36+
37+
@Override
38+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
39+
{
40+
if (!isInEditMode())
41+
{
42+
mHelper.adjustChildren();
43+
}
44+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
45+
}
2446
}

autolayout/src/main/java/com/zhy/autolayout/utils/AutoLayoutHelper.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import android.content.Context;
2020
import android.content.res.TypedArray;
2121
import android.util.AttributeSet;
22-
import android.util.TypedValue;
2322
import android.view.View;
2423
import android.view.ViewGroup;
2524

@@ -157,7 +156,7 @@ public static AutoLayoutInfo getAutoLayoutInfo(Context context,
157156
// String val = array.getString(index);
158157
// if (!isPxVal(val)) continue;
159158

160-
if (!isPxVal(array.peekValue(index))) continue;
159+
if (DimenUtils.isPxVal(array.peekValue(index))) continue;
161160

162161
int pxVal = 0;
163162
try
@@ -227,30 +226,6 @@ public static AutoLayoutInfo getAutoLayoutInfo(Context context,
227226
return info;
228227
}
229228

230-
private static boolean isPxVal(TypedValue val)
231-
{
232-
if (val != null && val.type == TypedValue.TYPE_DIMENSION &&
233-
getComplexUnit(val.data) == TypedValue.COMPLEX_UNIT_PX)
234-
{
235-
return true;
236-
}
237-
return false;
238-
}
239-
240-
private static int getComplexUnit(int data)
241-
{
242-
return TypedValue.COMPLEX_UNIT_MASK & (data >> TypedValue.COMPLEX_UNIT_SHIFT);
243-
}
244-
245-
private static boolean isPxVal(String val)
246-
{
247-
if (val.endsWith("px"))
248-
{
249-
return true;
250-
}
251-
return false;
252-
}
253-
254229
public interface AutoLayoutParams
255230
{
256231
AutoLayoutInfo getAutoLayoutInfo();

autolayout/src/main/java/com/zhy/autolayout/utils/AutoUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public static void autoPadding(View view)
6262
view.setPadding(l, t, r, b);
6363
}
6464

65+
public static boolean autoed(View view)
66+
{
67+
Object tag = view.getTag(R.id.id_tag_autolayout_size);
68+
if (tag != null) return true;
69+
view.setTag(R.id.id_tag_autolayout_size, "Just Identify");
70+
return false;
71+
}
72+
6573
public static void autoSize(View view)
6674
{
6775
ViewGroup.LayoutParams lp = view.getLayoutParams();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.zhy.autolayout.utils;
2+
3+
import android.util.TypedValue;
4+
5+
/**
6+
* Created by zhy on 16/3/3.
7+
*/
8+
public class DimenUtils
9+
{
10+
private static int getComplexUnit(int data)
11+
{
12+
return TypedValue.COMPLEX_UNIT_MASK & (data >> TypedValue.COMPLEX_UNIT_SHIFT);
13+
}
14+
15+
public static boolean isPxVal(TypedValue val)
16+
{
17+
if (val != null && val.type == TypedValue.TYPE_DIMENSION &&
18+
getComplexUnit(val.data) == TypedValue.COMPLEX_UNIT_PX)
19+
{
20+
return true;
21+
}
22+
return false;
23+
}
24+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':sample', ':autolayout', ':widgetsample'
1+
include ':sample', ':autolayout', ':widgetsample', ':autolayout-widget'

widgetsample/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
compile 'com.android.support:support-v4:23.1.0'
2425
compile 'com.android.support:appcompat-v7:23.1.0'
2526
compile project(':autolayout')
27+
compile project(':autolayout-widget')
28+
2629
}

0 commit comments

Comments
 (0)