Skip to content

Commit 7e64194

Browse files
author
hongyangAndroid
committed
增加扩展方式说明 issue#18 和 issue#21
1 parent e79dac5 commit 7e64194

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ ok,拿一些实际项目的页面,看下不同分辨率下的效果:
3939

4040
还有很多好处,比如上面的Item里面元素比较多,如果标识的比较全面,一个FrameLayout,里面的View填写各种marginLeft,marginTop就能完美实现,几乎不需要嵌套了。
4141

42-
## 用法
42+
## 引入
4343

4444
* Android Studio
4545

@@ -63,6 +63,8 @@ dependencies {
6363

6464
下载[AutoLayoutDemoForEclipse.zip](AutoLayoutDemoForEclipse.zip),导入到eclipse中即可。
6565

66+
## 用法
67+
6668
### 第一步:
6769

6870
在你的项目的AndroidManifest中注明你的`设计稿`的尺寸。
@@ -124,6 +126,62 @@ dependencies {
124126
1. 你们UI给的设计图的尺寸并非是主流的设计图,该尺寸没找到,你可以自己去新建一个设备。
125127
2. 不要在PreView中去查看所有分辨率下的显示,是看不出来适配效果的,因为有些计算是动态的。
126128

129+
## 扩展
130+
131+
对于其他继承系统的FrameLayout、LinearLayout、RelativeLayout的控件,比如`CardView`,如果希望再其内部直接支持"px"百分比化,可以自己扩展,扩展方式为下面的代码,也可参考[issue#21](https://github.com/hongyangAndroid/AndroidAutoLayout/issues/21)
132+
133+
```
134+
package com.zhy.sample.view;
135+
136+
import android.content.Context;
137+
import android.support.v7.widget.CardView;
138+
import android.util.AttributeSet;
139+
140+
import com.zhy.autolayout.AutoFrameLayout;
141+
import com.zhy.autolayout.utils.AutoLayoutHelper;
142+
143+
/**
144+
* Created by zhy on 15/12/8.
145+
*/
146+
public class AutoCardView extends CardView
147+
{
148+
private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);
149+
150+
public AutoCardView(Context context)
151+
{
152+
super(context);
153+
}
154+
155+
public AutoCardView(Context context, AttributeSet attrs)
156+
{
157+
super(context, attrs);
158+
}
159+
160+
public AutoCardView(Context context, AttributeSet attrs, int defStyleAttr)
161+
{
162+
super(context, attrs, defStyleAttr);
163+
}
164+
165+
@Override
166+
public AutoFrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
167+
{
168+
return new AutoFrameLayout.LayoutParams(getContext(), attrs);
169+
}
170+
171+
@Override
172+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
173+
{
174+
if (!isInEditMode())
175+
{
176+
mHelper.adjustChildren();
177+
}
178+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
179+
}
180+
181+
182+
}
183+
```
184+
127185

128186
## 注意事项
129187

sample/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ dependencies {
2424
compile 'com.android.support:appcompat-v7:23.1.0'
2525
compile project(':autolayout')
2626
compile 'com.android.support:design:23.1.0'
27+
compile 'com.android.support:cardview-v7:23.1.0'
2728
}

sample/sample.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
7373
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
7474
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.0/jars" />
75+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/cardview-v7/23.1.0/jars" />
7576
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/design/23.1.0/jars" />
7677
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.1.0/jars" />
7778
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.1.0/jars" />
@@ -96,6 +97,7 @@
9697
<orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
9798
<orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
9899
<orderEntry type="library" exported="" name="design-23.1.0" level="project" />
100+
<orderEntry type="library" exported="" name="cardview-v7-23.1.0" level="project" />
99101
<orderEntry type="library" exported="" name="support-v4-23.1.0" level="project" />
100102
<orderEntry type="module" module-name="autolayout" exported="" />
101103
</component>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.zhy.sample.view;
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+
}

0 commit comments

Comments
 (0)