安卓百分比布局之RelativeLayout
来源:互联网 发布:淘宝网婴幼儿童车 编辑:程序博客网 时间:2024/06/02 05:13
public class PencentLayout extends RelativeLayout { public PencentLayout(Context context) { super(context); } //这下解析XML文件然后把获取到的属性赋给控件 @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new LayoutParams(getContext(),attrs); } //测量宽高,此处主要用于获得父容器的宽高 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width= View.MeasureSpec.getSize(widthMeasureSpec);// int height= View.MeasureSpec.getSize(heightMeasureSpec); int count=getChildCount();//获得所有里面的子控件 for(int i=0;i<count;i++){ View child=getChildAt(i);//遍历每一个子控件 ViewGroup.LayoutParams layoutParams=child.getLayoutParams();//获得每一个子控件布局参数集合 float widthpercent=0; float heightpercent=0; if (layoutParams instanceof PencentLayout.LayoutParams ){//判断此类是否是 LayoutParams的实例 widthpercent=((LayoutParams) layoutParams).getWidthPercent();//如果是就获取他的宽度和高度 heightpercent=((LayoutParams) layoutParams).getHeightPercent(); } if (widthpercent!=0){//如果子控件没有使用这个属性责不进行修改 layoutParams.width= (int) (width*widthpercent);//修改子控件的宽度为父容器的宽度的百分比 } if (heightpercent!=0){ layoutParams.height= (int) (height*heightpercent); } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public static class LayoutParams extends RelativeLayout.LayoutParams{ private float widthPercent;//在布局参数里面多加上自己自定义的参数 private float heightPercent; public float getWidthPercent() { return widthPercent; } public float getHeightPercent() { return heightPercent; } public LayoutParams(Context c, AttributeSet attrs) { super(c, attrs); TypedArray array=c.obtainStyledAttributes(attrs,R.styleable.percentLayout);//获取所有参数集合 heightPercent=array.getFloat(R.styleable.percentLayout_layout_heightPercen,heightPercent);//获取自定义好的参数的值 widthPercent=array.getFloat(R.styleable.percentLayout_layout_widthPercen,widthPercent);//同上 array.recycle(); } }}
attr属性文件:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="percentLayout"> <attr name="layout_widthPercen" format="float"></attr> <attr name="layout_heightPercen" format="float"></attr> </declare-styleable></resources>
布局:
<com.example.administrator.myapplication.PencentLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" > <TextView android:text="@string/hello_world" android:layout_width="wrap_content" app:layout_heightPercen="0.5" app:layout_widthPercen="0.5" android:layout_centerHorizontal="true" android:gravity="center" android:background="#ff0000" android:layout_height="wrap_content" /></com.example.administrator.myapplication.PencentLayout>
0 0
- 安卓百分比布局之RelativeLayout
- 安卓 百分比布局
- 安卓百分比布局
- RelativeLayout实现百分比布局
- 安卓布局之相对布局(relativeLayout)
- 安卓相对布局RelativeLayout
- 安卓学习笔记之相对布局relativeLayout
- 安卓初学一之布局RelativeLayout和linearlayout
- RelativeLayout相对布局 安卓布局技巧
- RelativeLayout相对布局 安卓布局技巧
- 安卓布局 - 相对布局(RelativeLayout)
- Android Percent Layout 安卓百分比布局
- 安卓百分比布局的使用
- 安卓中的相对布局RelativeLayout
- 安卓布局:线性布局(Linearlayout)和相对布局(RelativeLayout)
- 安卓开发08:布局-相对位置布局 RelativeLayout
- 安卓布局基础笔记(相对布局 -- RelativeLayout )
- 安卓篇】安卓六大布局【四】:RelativeLayout布局
- 从Windows到Linux(二)
- js函数
- node-webkit js 复制粘贴
- C++之stl::string写时拷贝导致的问题
- kuangbin——线段树专题 F - Balanced Lineup
- 安卓百分比布局之RelativeLayout
- C语言二维数组(第十天)
- MATLAB使用基础
- oracle查询转换在mysql中实现对比
- 使用DbUtils实现增删改查——ResultSetHandler接口的实现类
- 实现memcpy函数
- 什么是熵?
- 胜者树与败者树
- 1077. Kuchiguse (20)-PAT甲级真题