如何获得的LinearLayout的高度 [英] How to get height of LinearLayout

查看:461
本文介绍了如何获得的LinearLayout的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 最后

这篇关于如何获得的LinearLayout的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆