Skip to content

Commit d9ad7cb

Browse files
author
hongyangAndroid
committed
增加尺寸参考指定宽度或者高度,简单修改用法
1 parent ce60e3f commit d9ad7cb

22 files changed

Lines changed: 736 additions & 274 deletions

README.md

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,46 @@ dependencies {
5151
}
5252
```
5353

54+
也可以直接
55+
56+
```
57+
dependencies {
58+
compile 'com.zhy:autolayout:1.2.0'
59+
}
60+
```
61+
62+
### 第一步:
63+
5464
在你的项目的AndroidManifest中注明你的`设计稿`的尺寸。
5565

5666
```xml
57-
<meta-data android:name="design_width" android:value="768"></meta-data>
58-
<meta-data android:name="design_height" android:value="1280"></meta-data>
67+
<meta-data android:name="design_width" android:value="768">
68+
</meta-data>
69+
<meta-data android:name="design_height" android:value="1280">
70+
</meta-data>
5971

6072
```
6173

62-
在你的Activity的onCreate里面写入:` AutoLayout.getInstance().auto(this, true);`
74+
### 第二步:
6375

64-
```xml
65-
@Override
66-
protected void onCreate(Bundle savedInstanceState)
67-
{
68-
super.onCreate(savedInstanceState);
69-
setContentView(R.layout.activity_main);
70-
71-
AutoLayout.getInstance().auto(this, true);
72-
}
73-
```
76+
让你的Activity继承自`AutoLayoutActivity`.
77+
78+
79+
非常简单的两个步骤,你就可以开始愉快的编写布局了,详细可以参考sample。
80+
81+
82+
83+
## 其他用法
7484

75-
在布局文件中编写时,将:
85+
如果你不希望继承`AutoLayoutActivity`,可以在编写布局文件时,将
7686

7787
* LinearLayout -> AutoLinearLayout
7888
* RelativeLayout -> AutoRelativeLayout
7989
* FrameLayout -> AutoFrameLayout
8090

91+
这样也可以完成适配。
92+
93+
8194
## 目前支持属性
8295

8396
* layout_width
@@ -89,14 +102,41 @@ dependencies {
89102

90103
## 注意事项
91104

92-
* TextView的高度问题
105+
### 1、 指定设置的值参考宽度或者高度
106+
107+
由于该库的特点,布局文件中宽高上的1px是不相等的,于是如果需要宽高保持一致的情况,布局中使用属性:
108+
109+
`app:layout_auto_basewidth="height"`,代表height上编写的像素值参考宽度。
110+
111+
`app:layout_auto_baseheight="width"`,代表width上编写的像素值参考高度。
112+
113+
如果需要指定多个值参考宽度即:
114+
115+
`app:layout_auto_basewidth="height|padding"`
116+
117+
用|隔开,类似gravity的用法,取值为:
118+
119+
* width,height
120+
* margin,marginLeft,marginTop,marginRight,marginBottom
121+
* padding,paddingLeft,paddingTop,paddingRight,paddingBottom
122+
* textSize.
123+
124+
### 2、将状态栏区域作为内容区域
125+
126+
如果某个Activity需要将状态栏区域作为实际的内容区域时,那么可用高度会变大,你所要做的只有一件事:让这个Activity实现`UseStatusBar`接口(仅仅作为标识左右,不需要实现任何方法),当然你肯定要自己开启`windowTranslucentStatus`或者设置`FLAG_TRANSLUCENT_STATUS`
127+
128+
注意:仅仅是改变状态栏颜色,并不需要实现此接口,因为并没有实际上增加可用高度。
129+
130+
131+
### 3、TextView的高度问题
93132

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

96135

97136

98137

99138

139+
100140
## 其他信息
101141

102142
作者信息:

autolayout/autolayout.iml

Lines changed: 4 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="AutoLayout" external.system.module.version="unspecified" 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.2.0" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>
@@ -65,6 +65,7 @@
6565
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
6666
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
6767
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
68+
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
6869
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
6970
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
7071
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
@@ -86,7 +87,9 @@
8687
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
8788
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
8889
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
90+
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
8991
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
92+
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
9093
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
9194
</content>
9295
<orderEntry type="jdk" jdkName="Android API 23 Platform (1)" jdkType="Android SDK" />

autolayout/build.gradle

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
apply plugin: 'com.android.library'
2+
apply plugin: 'com.github.dcendents.android-maven'
3+
apply plugin: 'com.jfrog.bintray'
4+
5+
version = "1.2.0"
26

37
android {
48
compileSdkVersion 23
59
buildToolsVersion "23.0.1"
610

711
defaultConfig {
8-
minSdkVersion 10
12+
minSdkVersion 7
913
targetSdkVersion 23
1014
versionCode 1
1115
versionName "1.0"
@@ -18,6 +22,79 @@ android {
1822
}
1923
}
2024

25+
def siteUrl = 'https://github.com/hongyangAndroid/AndroidAutoLayout' // #CONFIG# // project homepage
26+
def gitUrl = 'https://github.com/hongyangAndroid/AndroidAutoLayout.git' // #CONFIG# // project git
27+
group = "com.zhy"
28+
29+
install {
30+
repositories.mavenInstaller {
31+
// This generates POM.xml with proper parameters
32+
pom {
33+
project {
34+
packaging 'aar'
35+
name 'AutoLayout' // #CONFIG# // project title
36+
url siteUrl
37+
// Set your license
38+
licenses {
39+
license {
40+
name 'The Apache Software License, Version 2.0'
41+
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
42+
}
43+
}
44+
developers {
45+
developer {
46+
id 'hongyangAndroid' // #CONFIG# // your user id (you can write your nickname)
47+
name 'ZhangHongyang' // #CONFIG# // your user name
48+
email '623565791@qq.com' // #CONFIG# // your email
49+
}
50+
}
51+
scm {
52+
connection gitUrl
53+
developerConnection gitUrl
54+
url siteUrl
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
task sourcesJar(type: Jar) {
62+
from android.sourceSets.main.java.srcDirs
63+
classifier = 'sources'
64+
}
65+
66+
task javadoc(type: Javadoc) {
67+
source = android.sourceSets.main.java.srcDirs
68+
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
69+
}
70+
71+
task javadocJar(type: Jar, dependsOn: javadoc) {
72+
classifier = 'javadoc'
73+
from javadoc.destinationDir
74+
}
75+
76+
artifacts {
77+
archives javadocJar
78+
archives sourcesJar
79+
}
80+
Properties properties = new Properties()
81+
properties.load(project.rootProject.file('local.properties').newDataInputStream())
82+
bintray {
83+
user = properties.getProperty("bintray.user")
84+
key = properties.getProperty("bintray.apikey")
85+
configurations = ['archives']
86+
pkg {
87+
repo = "maven"
88+
name = "autolayout" // #CONFIG# project name in jcenter
89+
websiteUrl = siteUrl
90+
vcsUrl = gitUrl
91+
licenses = ["Apache-2.0"]
92+
publish = true
93+
}
94+
}
95+
96+
97+
2198
dependencies {
2299
compile fileTree(dir: 'libs', include: ['*.jar'])
23100
compile 'com.android.support:appcompat-v7:23.0.1'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.zhy.autolayout;
2+
3+
/**
4+
* Created by zhy on 15/12/1.
5+
*/
6+
public class AutoBase
7+
{
8+
public static final int WIDTH = 1;
9+
public static final int HEIGHT = WIDTH << 1;
10+
public static final int TEXTSIZE = HEIGHT << 1;
11+
public static final int PADDING = TEXTSIZE << 1;
12+
public static final int MARGIN = PADDING << 1;
13+
public static final int MARGIN_LEFT = MARGIN << 1;
14+
public static final int MARGIN_TOP = MARGIN_LEFT << 1;
15+
public static final int MARGIN_RIGHT = MARGIN_TOP << 1;
16+
public static final int MARGIN_BOTTOM = MARGIN_RIGHT << 1;
17+
public static final int PADDING_LEFT = MARGIN_BOTTOM << 1;
18+
public static final int PADDING_TOP = PADDING_LEFT << 1;
19+
public static final int PADDING_RIGHT = PADDING_TOP << 1;
20+
public static final int PADDING_BOTTOM = PADDING_RIGHT << 1;
21+
22+
23+
public static boolean contains(int autobase, int flag)
24+
{
25+
return (autobase & flag) != 0;
26+
}
27+
28+
29+
}

autolayout/src/main/java/com/zhy/autolayout/AutoLayoutActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class AutoLayoutActivity extends AppCompatActivity
1616
private static final String LAYOUT_FRAMELAYOUT = "FrameLayout";
1717
private static final String LAYOUT_RELATIVELAYOUT = "RelativeLayout";
1818

19+
1920
@Override
2021
public View onCreateView(String name, Context context, AttributeSet attrs)
2122
{

autolayout/src/main/java/com/zhy/autolayout/AutoLayout.java renamed to autolayout/src/main/java/com/zhy/autolayout/AutoLayoutConifg.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Created by zhy on 15/11/18.
1717
*/
18-
public class AutoLayout
18+
public class AutoLayoutConifg
1919
{
2020
private boolean mStatusBarAvailable;
2121
private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height";
@@ -70,7 +70,6 @@ public void auto(Activity activity, boolean ignoreStatusBar)
7070
mAvailaleHegiht = outMetrics.heightPixels;
7171
mAvailaleHegiht -= getStatusHeight(activity.getResources());
7272

73-
7473
if (ignoreStatusBar)
7574
return;
7675

@@ -150,26 +149,6 @@ private int getStatusHeight(Resources res)
150149
}
151150

152151

153-
private AutoLayout()
154-
{
155-
}
156-
157-
private static volatile AutoLayout sInstance;
158-
159-
public static AutoLayout getInstance()
160-
{
161-
if (sInstance == null)
162-
{
163-
synchronized (AutoLayout.class)
164-
{
165-
if (sInstance == null)
166-
{
167-
sInstance = new AutoLayout();
168-
}
169-
}
170-
}
171-
return sInstance;
172-
}
173152

174153

175154
}

0 commit comments

Comments
 (0)