手动膨胀视图上的layout()
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调用layout()之前需要先调用measure()。
You need to call measure() before you call layout().
首先,inflate 不会考虑您在 xml 中指定的布局参数。您可以通过使用来修复此问题,
这将确保您使用 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
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).