您的位置:首页 > 其它

自定义布局获取宽高问题

2016-01-05 11:40 232 查看
private void initView(){

        //初始化布局

        //假如想获取子布局高度

        View header = LayoutInflater.from(this).inflate(R.layout.main,null);

        measureView(header);

        int headerHeight = header.getMeasuredHeight();

        //其他操作

    }

    /**

     * 获取宽高前,要通知父布局自己需要占用的大小

     */

    private void measureView(View view) {

        ViewGroup.LayoutParams p = view.getLayoutParams();

        if (p == null) {

            p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        }

        int wid
8f41
th = ViewGroup.getChildMeasureSpec(0, 0, p.width);

        int height;

        int tempHeight = p.height;

        if (tempHeight > 0) {

            height = MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY);

        } else {

            height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECFIED);

        }

        view.measure(width, height);

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: