如果我在 onResume 中调用 getMeasuredWidth() 或 getWidth() 进行布局,它们将返回 0

发布于 2024-11-28 00:18:50 字数 185 浏览 2 评论 0原文

如果我在 onResume 中调用 getMeasuredWidth() 或 getWidth() 进行布局,它们将返回 0。 我认为此时此刻还没有绘制出该视图。

另外,我认为我需要将 getMeasuredWidth() 或 getWidth() 放在绘制布局并且视图测量已知后调用的回调方法中。 我应该使用什么android回调方法?

If I call getMeasuredWidth() or getWidth() for layout in onResume they returns 0.
I think that view it's not drawn yet in this moment.

Also I think that I need to put getMeasuredWidth() or getWidth() in callback method called after layout is drawn and view measurements are known.
What android callback method should I use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

淡淡的优雅 2024-12-05 00:18:50

您不能在之前的 View 上使用 width/height/getMeasuredWidth/getMeasuredHeight系统渲染它(通常来自 onCreate/onResume)。

简单的解决方案是将 Runnable 发布到布局。可运行对象将在 View 布局后执行。

BoxesLayout = (RelativeLayout) findViewById(R.id.BoxesLinearLayout);
BoxesLayout.post(new Runnable() {
    @Override
    public void run() {
        int w = BoxesLayout.getMeasuredWidth();
        int h = BoxesLayout.getMeasuredHeight();

        ...
    }
});

You cannot use the width/height/getMeasuredWidth/getMeasuredHeight on a View before the system renders it (typically from onCreate/onResume).

Simple solution for this is to post a Runnable to the layout. The runnable will be executed after the View has been laid out.

BoxesLayout = (RelativeLayout) findViewById(R.id.BoxesLinearLayout);
BoxesLayout.post(new Runnable() {
    @Override
    public void run() {
        int w = BoxesLayout.getMeasuredWidth();
        int h = BoxesLayout.getMeasuredHeight();

        ...
    }
});
神妖 2024-12-05 00:18:50

这个答案说:

使用View上的ViewTreeObserver来等待对于第一个布局。只有在第一个布局之后,getWidth()/getHeight()/getMeasuredWidth()/getMeasuredHeight() 才会起作用。

ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
  viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
      viewWidth = mediaGallery.getWidth();
      viewHeight = mediaGallery.getHeight();
    }
  });
}

This answer says:

Use the ViewTreeObserver on the View to wait for the first layout. Only after the first layout will getWidth()/getHeight()/getMeasuredWidth()/getMeasuredHeight() work.

ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
  viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
      viewWidth = mediaGallery.getWidth();
      viewHeight = mediaGallery.getHeight();
    }
  });
}
厌味 2024-12-05 00:18:50

您可以覆盖

you can override onLayout() in your view; this is used by android to position each of the children the view has, so you could do the stuff you want to do there after the super(..) call.

婴鹅 2024-12-05 00:18:50

事实上,如果布局未显示在屏幕上,则 getWidth() 方法返回 0。对我来说似乎有效的是在调用 getMeasuredWidth() 或 getMesuredHeight() 之前调用measure() 方法。对于测量方法,我使用 Layout.WRAP_CONTENT 参数来测量布局包含的内容。

希望这有帮助,
米哈伊

Indeed, it appears that if the layout is not shown on the screen the getWidth() method returns 0. What seems to work for me is calling the measure() method before calling the getMeasuredWidth() or getMesuredHeight(). For the measure method I used the Layout.WRAP_CONTENT arguments to get a measurement of what the layout contains.

Hope this helps,
Mihai

¢蛋碎的人ぎ生 2024-12-05 00:18:50

更简单的方法:

view.post(new Runnable() {
    public void run() {
        view.getWidth();
    }
}
);

http://developer.android .com/reference/android/view/View.html#post(java.lang.Runnable)

Simpler way:

view.post(new Runnable() {
    public void run() {
        view.getWidth();
    }
}
);

http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable)

零度℉ 2024-12-05 00:18:50

解决方案#1:
要动态地执行此操作,您需要一个标签。标签基本上是视图拥有记忆的一种方式。因此将convertView保存在另一个类(MyTag)中。
所以在你的java文件中:

private LayoutInflater layoutInflater;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MyTag holder = null;
    View row = convertView;
    if (row == null) {
      //Inflate the view however u can  
String strInflater = Context.LAYOUT_INFLATER_SERVICE;
        layoutInflater = (LayoutInflater) context.getSystemService(strInflater);
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResID, parent, false);
               holder = new MyTag();

            holder.itemName = (TextView) row.findViewById(R.id.example_itemname);
            holder.icon = (ImageView) row.findViewById(R.id.example_image);
            holder.button1 = (Button) row.findViewById(R.id.swipe_button1);
            holder.button2 = (Button) row.findViewById(R.id.swipe_button2);
            holder.button3 = (Button) row.findViewById(R.id.swipe_button3);
            row.setTag(holder);
        } else {
            holder = (MyTag) row.getTag();
        }

        holder.itemName.setText(itemdata.getItemName());
        System.out.println("holder.button3.getMeasuredWidth()= "+ holder.button3.getMeasuredWidth());
        System.out.println("holder.button3.getWidth()= "+ holder.button3.getWidth());

return row;
} //End of getView()


static class MyTag {  //It also works if not static

        TextView itemName;
        ImageView icon;
        Button button1;
        Button button2;
        Button button3;
    }

解决方案#2:
对其进行硬编码。预设宽度。
在你的 res/values/dimens.xml 文件中,包含

<dimen name="your_button_width">50dp</dimen>

然后在你的 res/layout/your_layout.xml 文件中,包含

        <Button  
         android:layout_width="@dimen/your_button_width"  />

然后在你的 java 文件中:

int buttonSize= (int) (context.getResources().getDimension(R.dimen.your_button_width));

Solution #1:
To do it dynamically, you need a tag. A tag is basically a way for views to have memories. So save the convertView in another class (MyTag).
So inside your java file:

private LayoutInflater layoutInflater;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MyTag holder = null;
    View row = convertView;
    if (row == null) {
      //Inflate the view however u can  
String strInflater = Context.LAYOUT_INFLATER_SERVICE;
        layoutInflater = (LayoutInflater) context.getSystemService(strInflater);
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResID, parent, false);
               holder = new MyTag();

            holder.itemName = (TextView) row.findViewById(R.id.example_itemname);
            holder.icon = (ImageView) row.findViewById(R.id.example_image);
            holder.button1 = (Button) row.findViewById(R.id.swipe_button1);
            holder.button2 = (Button) row.findViewById(R.id.swipe_button2);
            holder.button3 = (Button) row.findViewById(R.id.swipe_button3);
            row.setTag(holder);
        } else {
            holder = (MyTag) row.getTag();
        }

        holder.itemName.setText(itemdata.getItemName());
        System.out.println("holder.button3.getMeasuredWidth()= "+ holder.button3.getMeasuredWidth());
        System.out.println("holder.button3.getWidth()= "+ holder.button3.getWidth());

return row;
} //End of getView()


static class MyTag {  //It also works if not static

        TextView itemName;
        ImageView icon;
        Button button1;
        Button button2;
        Button button3;
    }

Solution #2:
Hard-code it. Pre-set the width.
Inside your res/values/dimens.xml file, include

<dimen name="your_button_width">50dp</dimen>

Then inside your res/layout/your_layout.xml file, include

        <Button  
         android:layout_width="@dimen/your_button_width"  />

Then inside your java file:

int buttonSize= (int) (context.getResources().getDimension(R.dimen.your_button_width));
月野兔 2024-12-05 00:18:50

使用下面的代码:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);
   Log.e("WIDTH",""+view.getWidth());
   Log.e("HEIGHT",""+view.getHeight());
}

Use below code:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);
   Log.e("WIDTH",""+view.getWidth());
   Log.e("HEIGHT",""+view.getHeight());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文