获取设置为包装内容的布局高度 [英] get height of layout that is set to wrap content

查看:84
本文介绍了获取设置为包装内容的布局高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取设置为包装内容的线性布局的高度.但它是零. 我尝试使用以下代码:

I want to get height of linear layout that is set to wrap content. but it is zero. i try this codes:

 final LinearLayout below= (LinearLayout) findViewById(R.id.below);


         ViewTreeObserver vto = below.getViewTreeObserver();  
         vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
             @Override  
             public void onGlobalLayout() {  
                  width  = below.getMeasuredWidth(); 
                  height = below.getMeasuredHeight();  

             }  
         });

和:

 LinearLayout below= (LinearLayout) findViewById(R.id.below);
       final int belowh=below.getHeight();

使用代码:

 final LinearLayout below= (LinearLayout) findViewById(R.id.below);
        ViewTreeObserver vto = below.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @Override 
            public void onGlobalLayout() { 

        if(below.getMeasuredHeight()> 0){

                 this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
                int width  = below.getMeasuredWidth();
                int height = below.getMeasuredHeight(); 
        }
            } 
        })

    ;

单击此处查看

推荐答案

尝试一下

final LinearLayout below= (LinearLayout) findViewById(R.id.below);
    ViewTreeObserver vto = below.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 

    if(below.getMeasuredHeight()> 0){

             this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
            int width  = below.getMeasuredWidth();
            int height = below.getMeasuredHeight(); 
    }
        } 
    })

;

这篇关于获取设置为包装内容的布局高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆