Android开发之控件宽高获取与设置

//获取屏幕宽高
        DisplayMetrics display = this.getResources().getDisplayMetrics();
        int width = display.widthPixels;
        int height = display.heightPixels;
        //获取Toolbar宽高
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        mToolbar.measure(w, h);
        int toolbarh = mToolbar.getMeasuredHeight();
        int toolbarw = mToolbar.getMeasuredWidth();
        //获取ll宽高
        ll.measure(w, h);
        int llh = ll.getMeasuredHeight();
        int llw = ll.getMeasuredWidth();
        //设置rl高度
        ViewGroup.LayoutParams pp = rl.getLayoutParams();
        pp.height = height - toolbarh - llh;
        rl.setLayoutParams(pp);

Guess you like

Origin blog.csdn.net/juer2017/article/details/123799955