在自定义 ViewGroup 上从布局 XML 扩充内容时,布局参数不适用
我使用 LayoutInflater
将布局添加到自定义 ViewGroup(不是 ...Layout
的子类,而是一个简单的 ViewGroup)。
经过短暂的战斗,并实施了 这篇文章,我设法让内容最终出现,但由于某种原因,它没有按照我想要的方式布局。在布局 XML 中,有 3 个元素需要水平分布,其中中心元素“推”边缘的元素。
下面是膨胀布局的代码:
private LayoutInflater inf = null;
private Paint backpaint = null;
private ViewGroup inflated = null;
private void inflate(Context context) {
this.setBackgroundColor(0x00000000);
inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflated = (ViewGroup)inf.inflate(R.layout.application_heading, null);
this.addView(inflated, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
inflated.measure(r, b);
inflated.layout(l, t, r, b);
}
起初我只添加了 inflated.layout(l, t, r, b) 但它并没有真正起作用。添加了 inflated.measure(r,b)
后,我终于看到了内容,但它们都粘在一起了,就好像 LinearLayout
是基础一样膨胀的 XML 设置为 WRAP_CONTENT 。
我尝试遍历 inflated
ViewGroup 的子级并对其调用 .layout(l,t,r,b)
,但这使它们拉伸以填充整个布局,一个在另一个之上。
我一定在这里遗漏了一些非常小的东西。有人知道它可能是什么吗?
I'm using the LayoutInflater
to add a layout into a custom ViewGroup (not a subclass of ...Layout
but a simple ViewGroup).
After a short battle with it, and after implementing all that was said in this post, I managed to make the content to finally appear, but for some reason it's not laid out as I want it to. In the layout XML there are 3 elements that need to distribute horizontally, with the central element "pushing" the ones on the edges.
Here's the code that's inflating the layout:
private LayoutInflater inf = null;
private Paint backpaint = null;
private ViewGroup inflated = null;
private void inflate(Context context) {
this.setBackgroundColor(0x00000000);
inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflated = (ViewGroup)inf.inflate(R.layout.application_heading, null);
this.addView(inflated, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
inflated.measure(r, b);
inflated.layout(l, t, r, b);
}
At first I only added the inflated.layout(l, t, r, b)
but it didn't really work. After I've added the inflated.measure(r,b)
, I finally saw the content, but it was all stuck together, as if the LinearLayout
that was the base for the inflated XML was set to WRAP_CONTENT
.
I tried to run through children of the inflated
ViewGroup and call .layout(l,t,r,b)
on them, but that made them stretch to fill the entire layout, one on top of the other.
I must be missing something really small here. Anyone knows what it might be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将包含类切换为
RelativeLayout
的子类,而不是ViewGroup
,一切都变得完美。Switched the containing class to subclass a
RelativeLayout
instead ofViewGroup
and everything became peachy.