频道栏目
首页 > 资讯 > Android > 正文

android 百分比布局

17-03-24        来源:[db:作者]  
收藏   我要投稿

android 百分比布局

public class PercentRelativeLayout extends RelativeLayout{

    public PercentRelativeLayout(Context context) {
        super(context);
    }

    public PercentRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PercentRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs); 
    }
    /**
     * 重写测量方法
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 先拿到父控件的宽高
        int width = View.MeasureSpec.getSize(widthMeasureSpec);
        int height = View.MeasureSpec.getSize(heightMeasureSpec);
        int count = this.getChildCount();
        for (int i = 0; i < count; i++) {// 循环迭代子控件
            View child = this.getChildAt(i);// 取出每一个子控件
            ViewGroup.LayoutParams lp = child.getLayoutParams();
            float widthPercent = 0;
            float hightPercent = 0;
            if (lp instanceof PercentRelativeLayout.LayoutParams) {// 支持百分比布局
                widthPercent = ((PercentRelativeLayout.LayoutParams) lp).widthPercent;
                hightPercent = ((PercentRelativeLayout.LayoutParams) lp).heightPercent;
            }
            if (widthPercent != 0) {
                // 父容器的宽*宽的百分比
                lp.width = (int) (width * widthPercent);
            }

            if (hightPercent != 0) {
                // 父容器的高*高的百分比
                lp.height = (int) (height * hightPercent);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 重写对子控件布局方法
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    /**
     * 重写对子控件布局属性进行获取解析
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
//      return super.generateLayoutParams(attrs);// 这里必须返回下面自定的LayoutParams
        return new LayoutParams(getContext(), attrs);
    }

    public static class LayoutParams extends RelativeLayout.LayoutParams{

        private float widthPercent;
        private float heightPercent;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            TypedArray a = c.obtainStyledAttributes(attrs,R.styleable.precentRelativeLayout);
            widthPercent = a.getFloat(R.styleable.precentRelativeLayout_layout_widthPrecent, widthPercent);
            heightPercent = a.getFloat(R.styleable.precentRelativeLayout_layout_heightPrecent, heightPercent);
            a.recycle();
        }

        public LayoutParams(int w, int h) {
            super(w, h);
        }

        public LayoutParams(android.view.ViewGroup.LayoutParams source) {
            super(source);
        }

        public LayoutParams(android.widget.RelativeLayout.LayoutParams source) {
            super(source);
        }

    }
}
<!--?xml version="1.0" encoding="utf-8"?-->
<resources>
    <declare-styleable name="precentRelativeLayout">
        </attr>
        </attr>
    </declare-styleable>
</resources>
com.castiel.demo.percentrelativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.castiel.demo" android:layout_width="match_parent" android:layout_height="match_parent">
 
    <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" app:layout_heightprecent="0.2" app:layout_widthprecent="0.2" android:background="#28FF28" android:text="http://blog.csdn.net/mynameishuangshuai">
 
    <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" app:layout_heightprecent="0.3" app:layout_widthprecent="0.3" android:background="#28FF28" android:text="猴子搬来的救兵">
 
    <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" app:layout_heightprecent="0.2" app:layout_widthprecent="0.2" android:background="#28FF28" android:text="castiel">
 
</textview></textview></textview></com.castiel.demo.percentrelativelayout>

 

相关TAG标签
上一篇:PopupWindow在onCreate()中直接show的异常
下一篇:Java垃圾回收和注意事项
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站