Android动态测量控件高度(Relatelayout,viewpager)并动态设置高度

         int w= View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        ll_send.measure(w, h);//这个是LinearLayout控件
        int ll_sendHeight =ll_send.getMeasuredHeight();
        ll_content.measure(w, h);
        int ll_contentHeight =ll_content.getMeasuredHeight();
viewpager.measure(all_w, all_h);
viewpager_sendHeight = viewpager.getMeasuredHeight();
private void measureHeight2(){//该方法用来动态设置viewpager高度

    tvbalayout2.measure(w, h);
    int tvbalayout2_sendHeight2 = tvbalayout2.getMeasuredHeight();
    //为ViewPager设置高度
    ViewGroup.LayoutParams params4 = viewpager.getLayoutParams();
    params4.height = rl_content2_sendHeight-tvbalayout2_sendHeight2/2;
    viewpager.setLayoutParams(params4);
}
有时候在oncreate方法中可能会测量不出控件的高度,这时候我们可以延时几秒钟才进行测量。
 handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        measureHeight2();
                    }
                },2000);


你可能感兴趣的:(Android学习之路)