Skip to content

Commit dbc974f

Browse files
author
hongyangAndroid
committed
完善demo,修复listview的item适配,增加ec的demo
1 parent d37260e commit dbc974f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+916
-534
lines changed

AutoLayoutDemoForEclipse.zip

4.3 MB
Binary file not shown.

README.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ so,看下用法:
1616

1717
你没有看错,拿到设计稿,在布局文件里面直接填写对应的px即可,px:这里的px并非是Google不建议使用的px,在内部会进行转化处理。
1818

19-
看下不同分辨率下的效果:
19+
ok,拿一些实际项目的页面,看下不同分辨率下的效果:
2020

21-
768*1280,Andriod 4.4.4
21+
左为:768 * 1280 ; 右为:1080 * 1920
2222

23+
<img src="preview/preview_01.png" width="800px"/>
2324

24-
<img src="autolayout_03.png" width="320px"/>
25+
<img src="preview/preview_02.png" width="800px"/>
2526

26-
480*800,Android 2.3.7
27+
<img src="preview/preview_03.png" width="800px"/>
2728

28-
<img src="autolayout_04.png" width="320px"/>
2929

3030

31-
上述两个机器的分辨率差距相当大了,但是完美实现了适配,最为重要的是:
31+
上述两个机器的分辨率差距挺大了,但是完美实现了适配,最为重要的是:
3232

3333
* 再也不用拿着设计稿去想这控件的宽高到底取多少dp
3434
* 再也不用去为多个屏幕去写多个dimens
@@ -55,10 +55,14 @@ dependencies {
5555

5656
```
5757
dependencies {
58-
compile 'com.zhy:autolayout:1.3.0'
58+
compile 'com.zhy:autolayout:1.3.2'
5959
}
6060
```
6161

62+
* Eclipse
63+
64+
下载[AutoLayoutDemoForEclipse.zip](AutoLayoutDemoForEclipse.zip),导入到eclipse中即可。
65+
6266
### 第一步:
6367

6468
在你的项目的AndroidManifest中注明你的`设计稿`的尺寸。
@@ -102,7 +106,35 @@ dependencies {
102106

103107
## 注意事项
104108

105-
### 1、 指定设置的值参考宽度或者高度
109+
### ListView、RecyclerView类的Item的适配
110+
111+
对于ListView这类控件的item,默认跟局部写“px”进行适配是无效的,因为外层非AutoXXXLayout,而是ListView。但是,不用怕,一行代码就可以支持了:
112+
113+
```java
114+
@Override
115+
public View getView(int position, View convertView, ViewGroup parent)
116+
{
117+
ViewHolder holder = null;
118+
if (convertView == null)
119+
{
120+
holder = new ViewHolder();
121+
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
122+
convertView.setTag(holder);
123+
//对于listview,注意添加这一行,即可在item上使用高度
124+
AutoUtils.autoSize(convertView);
125+
} else
126+
{
127+
holder = (ViewHolder) convertView.getTag();
128+
}
129+
130+
return convertView;
131+
}
132+
```
133+
134+
注意` AutoUtils.autoSize(convertView);`这行代码的位置即可。demo中也有相关实例。
135+
136+
137+
### 指定设置的值参考宽度或者高度
106138

107139
由于该库的特点,布局文件中宽高上的1px是不相等的,于是如果需要宽高保持一致的情况,布局中使用属性:
108140

@@ -121,7 +153,7 @@ dependencies {
121153
* padding,paddingLeft,paddingTop,paddingRight,paddingBottom
122154
* textSize.
123155

124-
### 2、TextView的高度问题
156+
### TextView的高度问题
125157

126158
设计稿一般只会标识一个字体的大小,比如你设置textSize="20px",实际上TextView所占据的高度肯定大于20px,字的上下都会有一定的建议,所以一定要灵活去写字体的高度,比如对于text上下的margin可以选择尽可能小一点。或者选择别的约束条件去定位(比如上例,选择了marginBottom)
127159

autolayout/autolayout.iml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.id=":autolayout" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.zhy" external.system.module.version="1.3.0" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.id=":autolayout" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.zhy" external.system.module.version="1.3.1" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>
@@ -90,6 +90,8 @@
9090
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
9191
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
9292
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
93+
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
94+
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
9395
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
9496
</content>
9597
<orderEntry type="jdk" jdkName="Android API 23 Platform (1)" jdkType="Android SDK" />

autolayout/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'com.github.dcendents.android-maven'
33
apply plugin: 'com.jfrog.bintray'
44

5-
version = "1.3.0"
5+
version = "1.3.2"
66

77
android {
88
compileSdkVersion 23
@@ -94,7 +94,6 @@ bintray {
9494
}
9595

9696

97-
9897
dependencies {
9998
compile fileTree(dir: 'libs', include: ['*.jar'])
10099
compile 'com.android.support:appcompat-v7:23.0.1'

autolayout/src/main/java/com/zhy/autolayout/config/AutoLayoutConifg.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ private AutoLayoutConifg()
3030
{
3131
}
3232

33+
public void checkParams()
34+
{
35+
if(mDesignHeight <=0 || mDesignWidth <= 0 )
36+
{
37+
throw new RuntimeException(
38+
"you must set " + KEY_DESIGN_WIDTH + " and " + KEY_DESIGN_HEIGHT + " in your manifest file.");
39+
}
40+
}
41+
42+
3343
public static AutoLayoutConifg getInstance()
3444
{
3545
return sIntance;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ public void adjustChildren()
121121
public static AutoLayoutInfo getAutoLayoutInfo(Context context,
122122
AttributeSet attrs)
123123
{
124+
125+
AutoLayoutConifg.getInstance().checkParams();
126+
124127
AutoLayoutInfo info = new AutoLayoutInfo();
125128

126129
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoLayout_Layout);

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.view.View;
44
import android.view.ViewGroup;
55

6+
import com.zhy.autolayout.R;
67
import com.zhy.autolayout.config.AutoLayoutConifg;
78

89
/**
@@ -25,11 +26,16 @@ public static void auto(View view)
2526

2627
public static void autoMargin(View view)
2728
{
28-
2929
if (!(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))
3030
return;
3131

3232
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
33+
if(lp == null)return ;
34+
35+
Object tag = view.getTag(R.id.id_tag_autolayout_margin);
36+
if (tag != null) return;
37+
view.setTag(R.id.id_tag_autolayout_margin, "Just Identify");
38+
3339
lp.leftMargin = getPercentWidthSize(lp.leftMargin);
3440
lp.topMargin = getPercentHeightSize(lp.topMargin);
3541
lp.rightMargin = getPercentWidthSize(lp.rightMargin);
@@ -39,6 +45,10 @@ public static void autoMargin(View view)
3945

4046
public static void autoPadding(View view)
4147
{
48+
Object tag = view.getTag(R.id.id_tag_autolayout_padding);
49+
if (tag != null) return;
50+
view.setTag(R.id.id_tag_autolayout_padding, "Just Identify");
51+
4252
int l = view.getPaddingLeft();
4353
int t = view.getPaddingTop();
4454
int r = view.getPaddingRight();
@@ -56,14 +66,29 @@ public static void autoSize(View view)
5666
{
5767
ViewGroup.LayoutParams lp = view.getLayoutParams();
5868

59-
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
60-
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
69+
if (lp == null) return;
70+
71+
Object tag = view.getTag(R.id.id_tag_autolayout_size);
72+
if (tag != null) return;
73+
74+
view.setTag(R.id.id_tag_autolayout_size, "Just Identify");
75+
76+
if(lp.width>0)
77+
{
78+
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
79+
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
80+
lp.width = (int) (lp.width * 1.0f / designWidth * screenWidth);
81+
}
82+
83+
if(lp.height>0)
84+
{
85+
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
86+
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
87+
lp.height = (int) (lp.height * 1.0f / designHeight * screenHeight);
88+
}
89+
6190

62-
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
63-
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
6491

65-
lp.width = (int) (lp.width * 1.0f / designWidth * screenWidth);
66-
lp.height = (int) (lp.height * 1.0f / designHeight / screenHeight);
6792

6893
}
6994

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class L
1212

1313
public static void e(String msg)
1414
{
15-
if (true)
15+
if (debug)
1616
{
1717
Log.e(TAG, msg);
1818
}

autolayout/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@
3636
</attr>
3737

3838
</declare-styleable>
39+
40+
3941
</resources>
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+
<item name="id_tag_autolayout_size" type="id"></item>
4+
<item name="id_tag_autolayout_padding" type="id"></item>
5+
<item name="id_tag_autolayout_margin" type="id"></item>
6+
</resources>

preview/preview_01.png

411 KB
Loading

preview/preview_02.png

274 KB
Loading

preview/preview_03.png

276 KB
Loading

sample/src/main/AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
android:theme="@style/AppTheme" >
1111
<activity
1212
android:screenOrientation="portrait"
13-
android:name=".CategoryActivity"
13+
android:name=".MainActivity"
1414
android:label="@string/app_name" >
1515
<intent-filter>
1616
<action android:name="android.intent.action.MAIN" />
@@ -21,10 +21,10 @@
2121

2222
<meta-data
2323
android:name="design_width"
24-
android:value="768" />
24+
android:value="1080" />
2525
<meta-data
2626
android:name="design_height"
27-
android:value="1280" />
27+
android:value="1920" />
2828

2929

3030
</application>

sample/src/main/java/com/zhy/sample/CategoryActivity.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.support.v4.view.ViewPager;
88

99
import com.zhy.autolayout.AutoLayoutActivity;
10+
import com.zhy.sample.fragment.SimpleFragment;
1011

1112
public class CategoryActivity extends AutoLayoutActivity
1213
{
@@ -33,11 +34,6 @@ protected void onCreate(Bundle savedInstanceState)
3334
@Override
3435
public Fragment getItem(int i)
3536
{
36-
switch (i)
37-
{
38-
case 1:
39-
return new SquareFragment();
40-
}
4137
return new SimpleFragment();
4238
}
4339

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.zhy.sample;
2+
3+
import android.os.Build.VERSION;
4+
import android.os.Build.VERSION_CODES;
5+
import android.os.Bundle;
6+
import android.support.v4.app.Fragment;
7+
import android.support.v4.app.FragmentManager;
8+
import android.support.v4.app.FragmentPagerAdapter;
9+
import android.support.v4.view.ViewPager;
10+
import android.view.WindowManager;
11+
12+
import com.zhy.autolayout.AutoLayoutActivity;
13+
import com.zhy.sample.fragment.ListFragment;
14+
import com.zhy.sample.fragment.PayFragment;
15+
import com.zhy.sample.fragment.RegisterFragment;
16+
17+
import java.util.ArrayList;
18+
19+
public class MainActivity extends AutoLayoutActivity
20+
{
21+
22+
private ViewPager mViewPager;
23+
24+
@Override
25+
protected void onCreate(Bundle savedInstanceState) {
26+
super.onCreate(savedInstanceState);
27+
//requestWindowFeature(Window.FEATURE_NO_TITLE);
28+
setImmersionStatus();
29+
setContentView(R.layout.activity_main);
30+
initView();
31+
initDatas();
32+
}
33+
34+
private void setImmersionStatus() {
35+
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
36+
// 透明状态栏
37+
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
38+
// 透明导航栏
39+
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
40+
}
41+
}
42+
43+
private void initDatas() {
44+
ArrayList<Fragment> mList = new ArrayList<Fragment>();
45+
mList.add(new ListFragment());
46+
mList.add(new RegisterFragment());
47+
mList.add(new PayFragment());
48+
mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), mList));
49+
}
50+
51+
private void initView() {
52+
mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
53+
}
54+
55+
public class MyAdapter extends FragmentPagerAdapter {
56+
ArrayList<Fragment> tabs = null;
57+
58+
public MyAdapter(FragmentManager fm, ArrayList<Fragment> tabs) {
59+
super(fm);
60+
this.tabs = tabs;
61+
}
62+
63+
@Override
64+
public Fragment getItem(int pos) {
65+
return tabs.get(pos);
66+
}
67+
68+
@Override
69+
public int getCount() {
70+
return tabs.size();
71+
}
72+
}
73+
74+
}

sample/src/main/java/com/zhy/sample/SquareFragment.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)