获取设置为wrap content的布局高度

3
我想要获取设置为包裹内容的线性布局的高度,但它是零。 我尝试了以下代码:
 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(); 
        }
            } 
        })

    ;

点击此处查看

该链接指向一个图片。

获取具有wrap_content属性的布局的高度 - Bhargava Mandapati
你应该在视图布局完成后调用它... - Md. Shahadat Sarker
@Md.ShahadatSarker 谢谢,它工作正常。 - coder android
尽量避免使用线程......如果不是必需的。 - Md. Shahadat Sarker
3个回答

6

在创建视图后,尝试使用以下代码获取布局的宽度和高度

final LinearLayout below= (LinearLayout) findViewById(R.id.below);
    below.post(new Runnable() 
        {
            @Override
            public void run()
            {
                Log.i("TAG", "Layout width :"+ below.getWidth());
                Log.i("TAG", "Layout height :"+ below.getHeight());
            }
    });

希望这可以帮助到您。

你的代码工作得很好,同时@Md . Shahadat Sarker的建议也起作用——在视图被布局后调用它——但我不知道哪一个更好。 - coder android

2

试试这个

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(); 
    }
        } 
    })

;

我之前尝试过,但不知道为什么下面这行无法解决:this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this); - coder android
我已编辑了我的回答,请现在检查。 - Mobile Apps Expert
我再次添加了您的代码,但问题仍然存在。您可以在问题中看到我的意思。我编辑了它并添加了一张图片。请点击链接查看问题所在。 - coder android
下面的代码中移除'this'关键字 >> below.getViewTreeObserver().removeGlobalOnLayoutListener(this); - Mobile Apps Expert

0

@coder android

如果你还卡在这个问题上......请看这个。我认为这会完美解决。

以下代码可以在如何检索视图的尺寸?中找到。

final TextView tv = (TextView)findViewById(R.id.image_test);
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        LayerDrawable ld = (LayerDrawable)tv.getBackground();
        ld.setLayerInset(1, 0, tv.getHeight() / 2, 0, 0);
        ViewTreeObserver obs = tv.getViewTreeObserver();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            obs.removeOnGlobalLayoutListener(this);
        } else {
            obs.removeGlobalOnLayoutListener(this);
        }
    }

});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接