Android: get width of Layout programatically having fill_parent in its xml

前端 未结 5 1780
误落风尘
误落风尘 2021-02-08 07:03

I have the a LinearLayout with width set in xml as fill_parent , now i need its width at runtime programatically. So i did this:

    Li         


        
相关标签:
5条回答
  • 2021-02-08 07:18

    You can try to listen to the globalLayout event, and get the width in there. You probably get the -1 because you are trying to get the width before the views are layed-out.

    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
           //Do it here
           LinearLayout layoutGet=(LinearLayout) findViewById(R.id.GameField1);
           LayoutParams layParamsGet= layoutGet.getLayoutParams();
           int width=layParamsGet.width;
           removeOnGlobalLayoutListener(layoutGet, this); // Assuming layoutGet is the View which you got the ViewTreeObserver from
        }
    });
    
    @SuppressLint("NewApi")
    public static void removeOnGlobalLayoutListener(View v, ViewTreeObserver.OnGlobalLayoutListener listener){
        if (Build.VERSION.SDK_INT < 16) v.getViewTreeObserver().removeGlobalOnLayoutListener(listener); 
        else v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
    }
    

    (vto is the view you want to get the width of)

    0 讨论(0)
  • 2021-02-08 07:18

    layoutGet.getWidth() should work out

    0 讨论(0)
  • 2021-02-08 07:27

    If you look at the value for FILL_PARENT (you should be using MATCH_PARENT since FILL_PARENT is now deprecated) you will notice the value for it is -1. LayoutParams are simply attributes for how your view should look. Once the view is inflated and looks the way the params specify, the view does not go back and change those Params to reflect the actual values of the view (width/height/etc).

    If you wanted to get the actual width of your view you would have to call getWidth() on your view once the layout has been inflated and displayed. Calling getWidth() before your layout has been displayed will result in 0.

    LinearLayout layoutGet=(LinearLayout) findViewById(R.id.GameField1);
    int width = layoutGet.getWidth();
    
    0 讨论(0)
  • 2021-02-08 07:28

    I got a simple approach working.

    myLayout = (RelativeLayout) findViewById(R.id.my_layout);
    myLayout.post(new Runnable() 
        {
    
            @Override
            public void run()
            {
                Log.i("TEST", "Layout width : "+ myLayout.getWidth());
    
            }
        });
    

    Jens Vossnack's approach mentioned below works fine. However, I found that the onGlobalLayout() method of GlobalLayoutListener is called repeatedly, which may not be appropriate in certain cases.

    0 讨论(0)
  • 2021-02-08 07:29

    First of all, you should use match_parent instead of fill_parent (deprecated).

    When you do int width=layParamsGet.width;, you take the right result which is -1 corresponding at fill_parent. If you wan't to get the real width, you need to wait onMesure() call and use

    LinearLayout layoutGet=(LinearLayout) findViewById(R.id.GameField1);
    int width = layoutGet. getMeasuredHeight();
    
    0 讨论(0)
提交回复
热议问题