因为百分比布局需要v7包支持,所以将百分比代码及其资源放到了v7包里。需要使用v7包的工程,直接使用该工程即可。
资源下载:http://download.csdn.net/download/qq_26149467/9606907
使用说明:
1.
AndroidManifest配置。
<meta-data android:name="design_width" android:value="768">
</meta-data>
<meta-data android:name="design_height" android:value="1280">
</meta-data>
2.
让你的Activity继承AutoLayoutActivity.(此时查看v4包是否统一),
也
可以在编写布局文件时,将
LinearLayout -> AutoLinearLayout
RelativeLayout -> AutoRelativeLayout
FrameLayout -> AutoFrameLayout
目前支持属性
layout_width
layout_height
layout_margin(left,top,right,bottom)
pading(left,top,right,bottom)
textSize
maxWidth, minWidth, maxHeight, minHeight
3.
主题继承Theme.AppCompat.Light.NoActionBar,可对其进行更改。
注意:
(1)默认使用的高度是设备的可用高度,也就是不包括状态栏和底部的操作栏的,如果你希望拿设备的物理高度进行百分比化:
可以在Application的onCreate方法中进行设置:
public class UseDeviceSizeApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();
AutoLayoutConifg.getInstance().useDeviceSize();
}
}
(2)对于
ListView的Item,需要增加
AutoUtils.autoSize(convertView);
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
convertView.setTag(holder);
//对于listview,注意添加这一行,即可在item上使用高度
AutoUtils.autoSize(convertView);
} else
{
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
(3)布局文件中宽高上的1px是不相等的,如果需要宽高保持一致的情况(比如正方形头像),布局中使用属性:
app:layout_auto_basewidth="height",代表height上编写的像素值参考宽度。
app:layout_auto_baseheight="width",代表width上编写的像素值参考高度。
如果需要指定多个值参考宽度即:
app:layout_auto_basewidth="height|padding"
用|隔开,类似gravity的用法,取值为:
width,height
margin,marginLeft,marginTop,marginRight,marginBottom
padding,paddingLeft,paddingTop,paddingRight,paddingBottom
textSize.
优点:通过这种方式,可以在很大程序上使用margin,也就是说,对于View的位置非常好控制,从而能够减少非常多的嵌套,甚至任何一个复杂的界面做到无嵌套。
几乎不需要去使用RelativeLayout的规则了,比如rightOf,完全可以由marginLeft完成。
对于LinearLayout的weight,几乎也不需要使用了,比如屏幕宽度720px,想要四个控件横向均分,完全可以写layout_width="180px"
使用举例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/包名"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0eff5" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="97.5px"
app:layout_auto_basewidth="height"
android:background="@color/maincolor"
android:gravity="center_vertical" >
<TextView
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="25px"
android:padding="5dp"
android:text="取消"
android:textColor="#ffffff" />
<TextView
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="25px"
android:padding="5dp"
android:text="提交"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="新增留宿"
android:textColor="#ffffff"
android:textSize="18dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="97.5px"
android:layout_marginTop="97.5px"
app:layout_auto_basewidth="height|marginTop"
android:background="#ebeced" >
<TextView
android:id="@+id/tv_show_type"
android:layout_width="65px"
android:layout_height="65px"
android:layout_centerVertical="true"
android:layout_marginLeft="25px"
app:layout_auto_basewidth="height|marginTop"
android:background="@drawable/edittext_background"
android:gravity="center"
android:text="座"
android:textColor="#000000"
android:textSize="@dimen/common_text_size" />
<TextView
android:id="@+id/tv_select_time"
android:layout_width="460px"
android:layout_height="65px"
android:layout_centerVertical="true"
android:layout_marginLeft="130px"
app:layout_auto_basewidth="height|marginLeft"
android:background="@drawable/edittext_background"
android:gravity="center"
android:text="选择日期"
android:textColor="#000000"
android:textSize="@dimen/common_text_size" />
<TextView
android:id="@+id/tv_select_num"
android:layout_width="wrap_content"
android:layout_height="65px"
android:layout_centerVertical="true"
android:layout_marginLeft="610px"
app:layout_auto_basewidth="height|marginTop"
android:gravity="center"
android:text="已选0人"
android:textColor="#000000"
android:textSize="@dimen/common_text_size" />
</RelativeLayout>
<cc.zenking.edu.zksc.activity.PinnedHeaderExpandableListView
android:id="@+id/explistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0.0dip"
android:layout_marginTop="195px"
app:layout_auto_basewidth="height|marginTop"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#00000000"
android:dividerHeight="0dp"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="false"
android:footerDividersEnabled="true"
android:groupIndicator="@null"
android:scrollbars="vertical"
android:scrollingCache="true" />
<RelativeLayout
android:id="@+id/rl_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="97.5px"
app:layout_auto_basewidth="height|marginTop"
android:visibility="gone" >
<ProgressBar
android:id="@+id/pb_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_nodata"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="97.5px"
app:layout_auto_basewidth="height|marginTop"
android:visibility="gone" >
<ImageView
android:id="@+id/iv_nodata"
android:layout_width="250px"
android:layout_height="250px"
android:layout_centerInParent="true"
app:layout_auto_basewidth="width|height"
android:src="@drawable/net_break" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_nodata"
android:layout_centerHorizontal="true"
android:text="获取数据失败,请重试~"
android:textColor="#bfbfbf"
android:textSize="@dimen/common_text_size" />
</RelativeLayout>
</RelativeLayout>
使用举例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/包名"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0eff5" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="97.5px"
app:layout_auto_basewidth="height"
android:background="@color/maincolor"
android:gravity="center_vertical" >
<TextView
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="25px"
android:padding="5dp"
android:text="取消"
android:textColor="#ffffff" />
<TextView
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="25px"
android:padding="5dp"
android:text="提交"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="新增留宿"
android:textColor="#ffffff"
android:textSize="18dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="97.5px"
android:layout_marginTop="97.5px"
app:layout_auto_basewidth="height|marginTop"
android:background="#ebeced" >
<TextView
android:id="@+id/tv_show_type"
android:layout_width="65px"
android:layout_height="65px"
android:layout_centerVertical="true"
android:layout_marginLeft="25px"
app:layout_auto_basewidth="height|marginTop"
android:background="@drawable/edittext_background"
android:gravity="center"
android:text="座"
android:textColor="#000000"
android:textSize="@dimen/common_text_size" />
<TextView
android:id="@+id/tv_select_time"
android:layout_width="460px"
android:layout_height="65px"
android:layout_centerVertical="true"
android:layout_marginLeft="130px"
app:layout_auto_basewidth="height|marginLeft"
android:background="@drawable/edittext_background"
android:gravity="center"
android:text="选择日期"
android:textColor="#000000"
android:textSize="@dimen/common_text_size" />
<TextView
android:id="@+id/tv_select_num"
android:layout_width="wrap_content"
android:layout_height="65px"
android:layout_centerVertical="true"
android:layout_marginLeft="610px"
app:layout_auto_basewidth="height|marginTop"
android:gravity="center"
android:text="已选0人"
android:textColor="#000000"
android:textSize="@dimen/common_text_size" />
</RelativeLayout>
<cc.zenking.edu.zksc.activity.PinnedHeaderExpandableListView
android:id="@+id/explistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0.0dip"
android:layout_marginTop="195px"
app:layout_auto_basewidth="height|marginTop"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#00000000"
android:dividerHeight="0dp"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="false"
android:footerDividersEnabled="true"
android:groupIndicator="@null"
android:scrollbars="vertical"
android:scrollingCache="true" />
<RelativeLayout
android:id="@+id/rl_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="97.5px"
app:layout_auto_basewidth="height|marginTop"
android:visibility="gone" >
<ProgressBar
android:id="@+id/pb_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_nodata"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="97.5px"
app:layout_auto_basewidth="height|marginTop"
android:visibility="gone" >
<ImageView
android:id="@+id/iv_nodata"
android:layout_width="250px"
android:layout_height="250px"
android:layout_centerInParent="true"
app:layout_auto_basewidth="width|height"
android:src="@drawable/net_break" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_nodata"
android:layout_centerHorizontal="true"
android:text="获取数据失败,请重试~"
android:textColor="#bfbfbf"
android:textSize="@dimen/common_text_size" />
</RelativeLayout>
</RelativeLayout>