RelativeLayout的高度的onCreate为0 [英] RelativeLayout height in onCreate is 0

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

问题描述

我试图来动态调整取决于(​​屏幕高度 - 我的布局高度),我行的高度/则为list.size

I 'm trying to dynamically adjust the height of my rows depending on the (screen height - my layout height) / list.size.

可惜在onCreate方法的布局高度返回null(没有,所以当我把它在上点击监听器)。

Unfortunately in the onCreate method the layout height returns null (not so when i call it in an on click listener).

有另一种方法,我可以调用它?
我想在运行时高度和宽度。

Is there another method I can call it in? I want Height and width at runtime.

帮助我....

在此先感谢

推荐答案

在的onCreate意见还没有被解雇,所以它返回null一如既往,你需要设置GlobalLayoutListener,然后在听者onGlobalLayout方法,你可以获取的实际尺寸查看/布局。

in onCreate views havent been laid, so it returns null always, you need to set GlobalLayoutListener, and then in onGlobalLayout method of the listener you can fetch actual dimensions of view/layout.

ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        //in here, place the code that requires you to know the dimensions.


         //this will be called as the layout is finished, prior to displaying.
    }
}

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

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