Skip to content

Commit

Permalink
add max/min width/height support
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyangAndroid committed Dec 24, 2015
1 parent 276fa91 commit 7cf68a0
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ dependencies {

## 配置

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

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

Expand Down Expand Up @@ -210,7 +210,7 @@ public class AutoCardView extends CardView

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

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

```java
@Override
Expand Down
2 changes: 1 addition & 1 deletion autolayout/autolayout.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand Down
4 changes: 4 additions & 0 deletions autolayout/src/main/java/com/zhy/autolayout/attr/Attrs.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ public interface Attrs
public static final int PADDING_TOP = PADDING_LEFT << 1;
public static final int PADDING_RIGHT = PADDING_TOP << 1;
public static final int PADDING_BOTTOM = PADDING_RIGHT << 1;
public static final int MIN_WIDTH = PADDING_BOTTOM << 1;
public static final int MAX_WIDTH = MIN_WIDTH << 1;
public static final int MIN_HEIGHT = MAX_WIDTH << 1;
public static final int MAX_HEIGHT = MIN_HEIGHT << 1;

}
17 changes: 17 additions & 0 deletions autolayout/src/main/java/com/zhy/autolayout/attr/AutoAttr.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ public abstract class AutoAttr
protected int baseWidth;
protected int baseHeight;

/*
protected boolean isBaseWidth;
protected boolean isBaseDefault;
public AutoAttr(int pxVal)
{
this.pxVal = pxVal;
isBaseDefault = true;
}
public AutoAttr(int pxVal, boolean isBaseWidth)
{
this.pxVal = pxVal;
this.isBaseWidth = isBaseWidth;
}
*/

public AutoAttr(int pxVal, int baseWidth, int baseHeight)
{
this.pxVal = pxVal;
Expand Down
37 changes: 0 additions & 37 deletions autolayout/src/main/java/com/zhy/autolayout/attr/AutoAttrEnum.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.zhy.autolayout.attr;

import android.view.View;

import java.lang.reflect.Method;

/**
* Created by zhy on 15/12/24.
*/
public class MaxHeightAttr extends AutoAttr
{
public MaxHeightAttr(int pxVal, int baseWidth, int baseHeight)
{
super(pxVal, baseWidth, baseHeight);
}

@Override
protected int attrVal()
{
return Attrs.MAX_HEIGHT;
}

@Override
protected boolean defaultBaseWidth()
{
return false;
}

@Override
protected void execute(View view, int val)
{
try
{
Method setMaxWidthMethod = view.getClass().getMethod("setMaxHeight", int.class);
setMaxWidthMethod.invoke(view, val);
} catch (Exception ignore)
{
}
}
}
40 changes: 40 additions & 0 deletions autolayout/src/main/java/com/zhy/autolayout/attr/MaxWidthAttr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.zhy.autolayout.attr;

import android.view.View;

import java.lang.reflect.Method;

/**
* Created by zhy on 15/12/24.
*/
public class MaxWidthAttr extends AutoAttr
{
public MaxWidthAttr(int pxVal, int baseWidth, int baseHeight)
{
super(pxVal, baseWidth, baseHeight);
}

@Override
protected int attrVal()
{
return Attrs.MAX_WIDTH;
}

@Override
protected boolean defaultBaseWidth()
{
return true;
}

@Override
protected void execute(View view, int val)
{
try
{
Method setMaxWidthMethod = view.getClass().getMethod("setMaxWidth", int.class);
setMaxWidthMethod.invoke(view, val);
} catch (Exception ignore)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.zhy.autolayout.attr;

import android.view.View;

import java.lang.reflect.Method;

/**
* Created by zhy on 15/12/24.
*/
public class MinHeightAttr extends AutoAttr
{
public MinHeightAttr(int pxVal, int baseWidth, int baseHeight)
{
super(pxVal, baseWidth, baseHeight);
}

@Override
protected int attrVal()
{
return Attrs.MIN_HEIGHT;
}

@Override
protected boolean defaultBaseWidth()
{
return false;
}

@Override
protected void execute(View view, int val)
{
try
{
Method setMaxWidthMethod = view.getClass().getMethod("setMinHeight", int.class);
setMaxWidthMethod.invoke(view, val);
} catch (Exception ignore)
{
}
}
}
40 changes: 40 additions & 0 deletions autolayout/src/main/java/com/zhy/autolayout/attr/MinWidthAttr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.zhy.autolayout.attr;

import android.view.View;

import java.lang.reflect.Method;

/**
* Created by zhy on 15/12/24.
*/
public class MinWidthAttr extends AutoAttr
{
public MinWidthAttr(int pxVal, int baseWidth, int baseHeight)
{
super(pxVal, baseWidth, baseHeight);
}

@Override
protected int attrVal()
{
return Attrs.MIN_WIDTH;
}

@Override
protected boolean defaultBaseWidth()
{
return true;
}

@Override
protected void execute(View view, int val)
{
try
{
Method setMaxWidthMethod = view.getClass().getMethod("setMinWidth", int.class);
setMaxWidthMethod.invoke(view, val);
} catch (Exception ignore)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
import com.zhy.autolayout.attr.MarginLeftAttr;
import com.zhy.autolayout.attr.MarginRightAttr;
import com.zhy.autolayout.attr.MarginTopAttr;
import com.zhy.autolayout.attr.MaxHeightAttr;
import com.zhy.autolayout.attr.MaxWidthAttr;
import com.zhy.autolayout.attr.MinHeightAttr;
import com.zhy.autolayout.attr.MinWidthAttr;
import com.zhy.autolayout.attr.PaddingAttr;
import com.zhy.autolayout.attr.PaddingBottomAttr;
import com.zhy.autolayout.attr.PaddingLeftAttr;
Expand Down Expand Up @@ -59,6 +63,11 @@ public class AutoLayoutHelper
android.R.attr.layout_marginTop,//
android.R.attr.layout_marginRight,//
android.R.attr.layout_marginBottom,//
android.R.attr.maxWidth,//
android.R.attr.maxHeight,//
android.R.attr.minWidth,//
android.R.attr.minHeight,//16843072


};

Expand All @@ -75,6 +84,11 @@ public class AutoLayoutHelper
private static final int INDEX_MARGIN_TOP = 10;
private static final int INDEX_MARGIN_RIGHT = 11;
private static final int INDEX_MARGIN_BOTTOM = 12;
private static final int INDEX_MAX_WIDTH = 13;
private static final int INDEX_MAX_HEIGHT = 14;
private static final int INDEX_MIN_WIDTH = 15;
private static final int INDEX_MIN_HEIGHT = 16;


/**
* move to other place?
Expand Down Expand Up @@ -194,7 +208,18 @@ public static AutoLayoutInfo getAutoLayoutInfo(Context context,
case INDEX_MARGIN_BOTTOM:
info.addAttr(new MarginBottomAttr(pxVal, baseWidth, baseHeight));
break;

case INDEX_MAX_WIDTH:
info.addAttr(new MaxWidthAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MAX_HEIGHT:
info.addAttr(new MaxHeightAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MIN_WIDTH:
info.addAttr(new MinWidthAttr(pxVal, baseWidth, baseHeight));
break;
case INDEX_MIN_HEIGHT:
info.addAttr(new MinHeightAttr(pxVal, baseWidth, baseHeight));
break;
}
}
array.recycle();
Expand Down
4 changes: 4 additions & 0 deletions autolayout/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<flag name="paddingTop" value="1024"></flag>
<flag name="paddingRight" value="2048"></flag>
<flag name="paddingBottom" value="4096"></flag>
<flag name="minWidth" value="8192"></flag>
<flag name="maxWidth" value="16384"></flag>
<flag name="minHeight" value="32768"></flag>
<flag name="maxHeight" value="65536"></flag>
</attr>

</declare-styleable>
Expand Down

0 comments on commit 7cf68a0

Please sign in to comment.