手动膨胀视图上的layout()

发布于 2024-10-26 14:35:15 字数 1850 浏览 1 评论 0原文

Activity 中,我手动从 XML 扩充 View 对象,如下所示:

    LayoutInflater inflater = LayoutInflater.from (this);
    View topView = inflater.inflate (R.layout.topview_layout, null);

我不会将视图添加到任何显示层次结构中,而是使用它来生成位图然后我将显示它。由于这些位图将是我的(主)屏幕的大小,因此我尝试导致布局发生:(

    Display display = ((WindowManager) getSystemService (Context.WINDOW_SERVICE))
                        .getDefaultDisplay();
    topView.measure (display.getWidth (), display.getHeight ());
    topView.layout (0, 0, display.getWidth (), display.getHeight ());

它在上面的代码中获得了正确的显示大小。)

topView 是一个 < code>LinearLayout 其中包含一些其他视图。其中之一是 ImageView

    ImageView childView = (ImageView) pageView.findViewById (R.id.textArea);

但是,在调用 layout() 后,该视图似乎永远不会被告知其尺寸:(

    int childViewWidth = childView.getWidth ();
    int childViewHeight = childView.getHeight ();

上述代码检索到的尺寸是0,0.)

所有上述代码都是线性发生的,并且是从我的 Activity 子类的 onCreate() 方法中调用的。

我正在测试的布局 XML 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/topView"
        android:orientation="vertical"
        android:background="@color/blue"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ImageView
        android:id="@+id/textArea"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gold"/>
</LinearLayout>

我非常感谢任何线索!

注意:编辑以添加布局 XML 和 罗曼盖伊

In an Activity, I am manually inflating a View object from XML as follows:

    LayoutInflater inflater = LayoutInflater.from (this);
    View topView = inflater.inflate (R.layout.topview_layout, null);

I won't be adding the view to any display hierarchy, but rather using it to generate bitmaps which I will then display. Since those bitmaps will be the size of my (main) screen, I try to cause the layout to occur:

    Display display = ((WindowManager) getSystemService (Context.WINDOW_SERVICE))
                        .getDefaultDisplay();
    topView.measure (display.getWidth (), display.getHeight ());
    topView.layout (0, 0, display.getWidth (), display.getHeight ());

(It is getting the proper size of the display in the above code.)

topView is a LinearLayout which contains a few other views. One of them is an ImageView:

    ImageView childView = (ImageView) pageView.findViewById (R.id.textArea);

However, this view never seems to be informed about its dimensions after the call to layout():

    int childViewWidth = childView.getWidth ();
    int childViewHeight = childView.getHeight ();

(The dimensions retrieved by the above code are 0,0.)

All of the above code occurs linearly, and is called from within my Activity subclass' onCreate() method.

The layout XML I'm testing with is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/topView"
        android:orientation="vertical"
        android:background="@color/blue"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ImageView
        android:id="@+id/textArea"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gold"/>
</LinearLayout>

I would most appreciate any clues!

NOTE: Edited to add the layout XML and the measure() call pointed out as missing by Romain Guy.

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

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

发布评论

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

评论(2

我家小可爱 2024-11-02 14:35:15

在调用layout()之前需要先调用measure()。

You need to call measure() before you call layout().

两仪 2024-11-02 14:35:15

首先,inflate 不会考虑您在 xml 中指定的布局参数。您可以通过使用来修复此问题,

inflater.inflate( R.layout.top_view_layout /* resource id */,
                  parentView,
                  false /* attachToRoot */);

这将确保您使用 match_parent 来指定宽度和高度。

或者

如果您不想这样做,那么您应该首先为膨胀视图设置 LayoutParams。

PS:据我所知,布局方法将采用可用于显示的屏幕区域参数(左、上、右、下),但它会考虑用于显示的视图的原始大小(在您的情况下为 0,0 )。

First thing is inflate won't consider the layout parameters that you have specified in the xml. You can fix that by using

inflater.inflate( R.layout.top_view_layout /* resource id */,
                  parentView,
                  false /* attachToRoot */);

This will make sure that you are using the match_parent for both width and height.

OR

If you don't want to do that, then you should set the LayoutParams for the inflated view first.

P.S.: AFAIK, the layout method will take the parameters which are screen area available for display (left, top, right, bottom), but it will consider original size of the view for displaying (which is 0,0 in your case).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文