Skip to content

Commit 7cf68a0

Browse files
author
hongyangAndroid
committed
add max/min width/height support
1 parent 276fa91 commit 7cf68a0

File tree

11 files changed

+214
-41
lines changed

11 files changed

+214
-41
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ dependencies {
111111

112112
## 配置
113113

114-
默认使用的宽度是设备的可用高度,也就是不包括状态栏和底部的操作栏的,如果你希望拿设备的物理高度进行百分比化:
114+
默认使用的高度是设备的可用高度,也就是不包括状态栏和底部的操作栏的,如果你希望拿设备的物理高度进行百分比化:
115115

116116
可以在Application的onCreate方法中进行设置:
117117

@@ -210,7 +210,7 @@ public class AutoCardView extends CardView
210210

211211
### ListView、RecyclerView类的Item的适配
212212

213-
对于ListView这类控件的item,默认跟局部写“px”进行适配是无效的,因为外层非AutoXXXLayout,而是ListView。但是,不用怕,一行代码就可以支持了:
213+
对于ListView这类控件的item,默认根局部写“px”进行适配是无效的,因为外层非AutoXXXLayout,而是ListView。但是,不用怕,一行代码就可以支持了:
214214

215215
```java
216216
@Override

autolayout/autolayout.iml

Lines changed: 1 addition & 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.4" 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.5" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>

autolayout/src/main/java/com/zhy/autolayout/attr/Attrs.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@ public interface Attrs
2020
public static final int PADDING_TOP = PADDING_LEFT << 1;
2121
public static final int PADDING_RIGHT = PADDING_TOP << 1;
2222
public static final int PADDING_BOTTOM = PADDING_RIGHT << 1;
23+
public static final int MIN_WIDTH = PADDING_BOTTOM << 1;
24+
public static final int MAX_WIDTH = MIN_WIDTH << 1;
25+
public static final int MIN_HEIGHT = MAX_WIDTH << 1;
26+
public static final int MAX_HEIGHT = MIN_HEIGHT << 1;
2327

2428
}

autolayout/src/main/java/com/zhy/autolayout/attr/AutoAttr.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ public abstract class AutoAttr
1515
protected int baseWidth;
1616
protected int baseHeight;
1717

18+
/*
19+
protected boolean isBaseWidth;
20+
protected boolean isBaseDefault;
21+
22+
public AutoAttr(int pxVal)
23+
{
24+
this.pxVal = pxVal;
25+
isBaseDefault = true;
26+
}
27+
28+
public AutoAttr(int pxVal, boolean isBaseWidth)
29+
{
30+
this.pxVal = pxVal;
31+
this.isBaseWidth = isBaseWidth;
32+
}
33+
*/
34+
1835
public AutoAttr(int pxVal, int baseWidth, int baseHeight)
1936
{
2037
this.pxVal = pxVal;

autolayout/src/main/java/com/zhy/autolayout/attr/AutoAttrEnum.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.zhy.autolayout.attr;
2+
3+
import android.view.View;
4+
5+
import java.lang.reflect.Method;
6+
7+
/**
8+
* Created by zhy on 15/12/24.
9+
*/
10+
public class MaxHeightAttr extends AutoAttr
11+
{
12+
public MaxHeightAttr(int pxVal, int baseWidth, int baseHeight)
13+
{
14+
super(pxVal, baseWidth, baseHeight);
15+
}
16+
17+
@Override
18+
protected int attrVal()
19+
{
20+
return Attrs.MAX_HEIGHT;
21+
}
22+
23+
@Override
24+
protected boolean defaultBaseWidth()
25+
{
26+
return false;
27+
}
28+
29+
@Override
30+
protected void execute(View view, int val)
31+
{
32+
try
33+
{
34+
Method setMaxWidthMethod = view.getClass().getMethod("setMaxHeight", int.class);
35+
setMaxWidthMethod.invoke(view, val);
36+
} catch (Exception ignore)
37+
{
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.zhy.autolayout.attr;
2+
3+
import android.view.View;
4+
5+
import java.lang.reflect.Method;
6+
7+
/**
8+
* Created by zhy on 15/12/24.
9+
*/
10+
public class MaxWidthAttr extends AutoAttr
11+
{
12+
public MaxWidthAttr(int pxVal, int baseWidth, int baseHeight)
13+
{
14+
super(pxVal, baseWidth, baseHeight);
15+
}
16+
17+
@Override
18+
protected int attrVal()
19+
{
20+
return Attrs.MAX_WIDTH;
21+
}
22+
23+
@Override
24+
protected boolean defaultBaseWidth()
25+
{
26+
return true;
27+
}
28+
29+
@Override
30+
protected void execute(View view, int val)
31+
{
32+
try
33+
{
34+
Method setMaxWidthMethod = view.getClass().getMethod("setMaxWidth", int.class);
35+
setMaxWidthMethod.invoke(view, val);
36+
} catch (Exception ignore)
37+
{
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.zhy.autolayout.attr;
2+
3+
import android.view.View;
4+
5+
import java.lang.reflect.Method;
6+
7+
/**
8+
* Created by zhy on 15/12/24.
9+
*/
10+
public class MinHeightAttr extends AutoAttr
11+
{
12+
public MinHeightAttr(int pxVal, int baseWidth, int baseHeight)
13+
{
14+
super(pxVal, baseWidth, baseHeight);
15+
}
16+
17+
@Override
18+
protected int attrVal()
19+
{
20+
return Attrs.MIN_HEIGHT;
21+
}
22+
23+
@Override
24+
protected boolean defaultBaseWidth()
25+
{
26+
return false;
27+
}
28+
29+
@Override
30+
protected void execute(View view, int val)
31+
{
32+
try
33+
{
34+
Method setMaxWidthMethod = view.getClass().getMethod("setMinHeight", int.class);
35+
setMaxWidthMethod.invoke(view, val);
36+
} catch (Exception ignore)
37+
{
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.zhy.autolayout.attr;
2+
3+
import android.view.View;
4+
5+
import java.lang.reflect.Method;
6+
7+
/**
8+
* Created by zhy on 15/12/24.
9+
*/
10+
public class MinWidthAttr extends AutoAttr
11+
{
12+
public MinWidthAttr(int pxVal, int baseWidth, int baseHeight)
13+
{
14+
super(pxVal, baseWidth, baseHeight);
15+
}
16+
17+
@Override
18+
protected int attrVal()
19+
{
20+
return Attrs.MIN_WIDTH;
21+
}
22+
23+
@Override
24+
protected boolean defaultBaseWidth()
25+
{
26+
return true;
27+
}
28+
29+
@Override
30+
protected void execute(View view, int val)
31+
{
32+
try
33+
{
34+
Method setMaxWidthMethod = view.getClass().getMethod("setMinWidth", int.class);
35+
setMaxWidthMethod.invoke(view, val);
36+
} catch (Exception ignore)
37+
{
38+
}
39+
}
40+
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
import com.zhy.autolayout.attr.MarginLeftAttr;
3232
import com.zhy.autolayout.attr.MarginRightAttr;
3333
import com.zhy.autolayout.attr.MarginTopAttr;
34+
import com.zhy.autolayout.attr.MaxHeightAttr;
35+
import com.zhy.autolayout.attr.MaxWidthAttr;
36+
import com.zhy.autolayout.attr.MinHeightAttr;
37+
import com.zhy.autolayout.attr.MinWidthAttr;
3438
import com.zhy.autolayout.attr.PaddingAttr;
3539
import com.zhy.autolayout.attr.PaddingBottomAttr;
3640
import com.zhy.autolayout.attr.PaddingLeftAttr;
@@ -59,6 +63,11 @@ public class AutoLayoutHelper
5963
android.R.attr.layout_marginTop,//
6064
android.R.attr.layout_marginRight,//
6165
android.R.attr.layout_marginBottom,//
66+
android.R.attr.maxWidth,//
67+
android.R.attr.maxHeight,//
68+
android.R.attr.minWidth,//
69+
android.R.attr.minHeight,//16843072
70+
6271

6372
};
6473

@@ -75,6 +84,11 @@ public class AutoLayoutHelper
7584
private static final int INDEX_MARGIN_TOP = 10;
7685
private static final int INDEX_MARGIN_RIGHT = 11;
7786
private static final int INDEX_MARGIN_BOTTOM = 12;
87+
private static final int INDEX_MAX_WIDTH = 13;
88+
private static final int INDEX_MAX_HEIGHT = 14;
89+
private static final int INDEX_MIN_WIDTH = 15;
90+
private static final int INDEX_MIN_HEIGHT = 16;
91+
7892

7993
/**
8094
* move to other place?
@@ -194,7 +208,18 @@ public static AutoLayoutInfo getAutoLayoutInfo(Context context,
194208
case INDEX_MARGIN_BOTTOM:
195209
info.addAttr(new MarginBottomAttr(pxVal, baseWidth, baseHeight));
196210
break;
197-
211+
case INDEX_MAX_WIDTH:
212+
info.addAttr(new MaxWidthAttr(pxVal, baseWidth, baseHeight));
213+
break;
214+
case INDEX_MAX_HEIGHT:
215+
info.addAttr(new MaxHeightAttr(pxVal, baseWidth, baseHeight));
216+
break;
217+
case INDEX_MIN_WIDTH:
218+
info.addAttr(new MinWidthAttr(pxVal, baseWidth, baseHeight));
219+
break;
220+
case INDEX_MIN_HEIGHT:
221+
info.addAttr(new MinHeightAttr(pxVal, baseWidth, baseHeight));
222+
break;
198223
}
199224
}
200225
array.recycle();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
<flag name="paddingTop" value="1024"></flag>
3434
<flag name="paddingRight" value="2048"></flag>
3535
<flag name="paddingBottom" value="4096"></flag>
36+
<flag name="minWidth" value="8192"></flag>
37+
<flag name="maxWidth" value="16384"></flag>
38+
<flag name="minHeight" value="32768"></flag>
39+
<flag name="maxHeight" value="65536"></flag>
3640
</attr>
3741

3842
</declare-styleable>

0 commit comments

Comments
 (0)