简体   繁体   English

为什么LinearLayout子getWidth方法返回0?

[英]Why does LinearLayout child getWidth method return 0?

I have a LinearLayout, and this LinearLayout will hold dynamically placed views. 我有一个LinearLayout,这个LinearLayout将保存动态放置的视图。 I need to find out what the width of the children of LinearLayout, however this has to be done in onCreate method. 我需要找出LinearLayout子元素的宽度,但是必须在onCreate方法中完成。 From researching I've found out that you can't use getWidth from this method. 从研究中我发现你不能使用这种方法的getWidth。 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. 所以我使用onWindowFocusChanged,它适用于父LinearLayout(返回实际大小),但它不适用于它的子节点。

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();

inside the onCreate , views still can't know the state of the nearby views and the children ,etc... so only after all is prepared and the layout process is done , you can get the size of views . 在onCreate中,视图仍然无法知道附近视图和子项等的状态...所以只有在准备好并且布局过程完成后,您才能获得视图的大小。

here's a quick code for getting the size of the view just before it's being drawn: 这是一个快速的代码,用于在绘制之前获取视图的大小:

private static void runJustBeforeBeingDrawn(final View view, final Runnable runnable)
{
    final ViewTreeObserver vto = view.getViewTreeObserver();
    final OnPreDrawListener preDrawListener = new OnPreDrawListener()
    {
        @Override
        public boolean onPreDraw()
        {
            Log.d(App.APPLICATION_TAG, CLASS_TAG + "onpredraw");
            runnable.run();
            final ViewTreeObserver vto = view.getViewTreeObserver();
            vto.removeOnPreDrawListener(this);
            return true;
        }
    };
    vto.addOnPreDrawListener(preDrawListener);
}

alternatively , you can use addOnGlobalLayoutListener instead of addOnPreDrawListener if you wish. 或者,如果您愿意,可以使用addOnGlobalLayoutListener而不是addOnPreDrawListener

example of usage : 用法示例:

runJustBeforeBeingDrawn(view,new Runnable()
{
  @Override
  public void run()
  {
    int width=view.getWidth();
    int height=view.getHeight();
  }
});

another approach is to use onWindowFocusChanged (and check that hasFocus==true) , but that's not always the best way ( only use for simple views-creation, not for dynamic creations) 另一种方法是使用onWindowFocusChanged(并检查hasFocus == true),但这并不总是最好的方法(仅用于创建简单视图,而不用于动态创建)

EDIT: Alternative to runJustBeforeBeingDrawn: https://stackoverflow.com/a/28136027/878126 编辑:runJustBeforeBeingDrawn的替代方案: https ://stackoverflow.com/a/28136027/878126

https://stackoverflow.com/a/3594216/1397218 https://stackoverflow.com/a/3594216/1397218

So you should somehow change your logic. 所以你应该以某种方式改变你的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM