小编cod*_*oid的帖子

获取设置为包装内容的布局高度

我想获得设置为包装内容的线性布局的高度。但它是零。我试试这个代码:

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

             }  
         });
Run Code Online (Sandbox Code Playgroud)

和 :

 LinearLayout below= (LinearLayout) findViewById(R.id.below);
       final int belowh=below.getHeight();
Run Code Online (Sandbox Code Playgroud)

使用代码:

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

    ;
Run Code Online (Sandbox Code Playgroud)

点击这里查看

android android-linearlayout

3
推荐指数
1
解决办法
4441
查看次数

标签 统计

android ×1

android-linearlayout ×1