如何获得的LinearLayout的高度如何获得、高度、LinearLayout

2023-09-13 01:27:41 作者:樱桃奶球

我有一个LinearLayout中设置高度如下match_parent:

I have a LinearLayout set height as match_parent as below:

<LinearLayout
    android:id="@+id/list_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

我想这个的LinearLayout的高度。 我用下面的code:

I want to get the height of this LinearLayout. I used the code below:

LinearLayout ll_list = (LinearLayout)findViewById(R.id.list_layout);
int h = ll_list.getHeight();

但它返回null。 我能怎么做?

But it return null. How can I do?

推荐答案

首先:你的的LinearLayout id是 left_layout ,不是 list_layout

First of all: your LinearLayout id is left_layout, not list_layout.

此外, ll_list.getHeight()将返回0(以及 ll_list.getWidth()),如果它不是drawed呢。

Also, ll_list.getHeight() will return 0 (as well as ll_list.getWidth()) if it's not drawed yet.

解决方案将这个高度后,您的观点是layouted:

Solution would be to get the height after your view is layouted:

ll_list.post(new Runnable(){
    public void run(){
         int height = ll_list.getHeight();
    }
});

和确保你的 ll_list 最后