在自定义 ViewGroup 上从布局 XML 扩充内容时,布局参数不适用

发布于 2025-01-04 10:49:34 字数 1305 浏览 1 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(1

坏尐絯 2025-01-11 10:49:34

将包含类切换为 RelativeLayout 的子类,而不是 ViewGroup,一切都变得完美。

Switched the containing class to subclass a RelativeLayout instead of ViewGroup and everything became peachy.

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