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

前端 未结 5 1838
误落风尘
误落风尘 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: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();
    

提交回复
热议问题