为什么的LinearLayout孩子的getWidth方法返回0? [英] Why does LinearLayout child getWidth method return 0?

查看:140
本文介绍了为什么的LinearLayout孩子的getWidth方法返回0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的LinearLayout,这的LinearLayout将举行动态放置的看法。我需要找出的LinearLayout的孩子的宽度,但是这有什么,在onCreate方法进行。从研究,我发现,你不能从这种方法使用的getWidth。所以不是我使用onWindowFocusChanged,其中工程父的LinearLayout(返回我的实际大小),但它不与它的孩子们的工作。

I have a LinearLayout, and this LinearLayout will hold dynamically placed views. I need to find out what the width of the children of LinearLayout, however this has to be done in onCreate method. From researching I've found out that you can't use getWidth from this method. So instead I'm using onWindowFocusChanged, which works for the parent LinearLayout (returning me the actual size), but it doesn't work with its children.

另一件事我注意到的是,当屏幕消失,屏幕被锁定,我可以在返回的儿童的实际宽度日志(我认为该项活动已暂停)。

Another thing I noticed is that when the screen is fading away and the screen is locked, I can see at the logs the actual width of the children being returned (I think the activity is being paused).

我很坚持,这是必要的,因为我需要动态地把根据孩子的宽度这些看法。

I'm really stuck and this is needed because I need to dynamically place those views depending on the children width.

推荐答案

您可能能够获得与下文。但正如有人指出,这可能不是一个好主意。

You might be able to get with the below. But as others pointed out, this probably isn't a great idea.

LinearLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
LinearLayout.getMeasuredWidth();

这篇关于为什么的LinearLayout孩子的getWidth方法返回0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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