ViewStub 具有目标布局的自定义 View 属性

发布于 2024-09-06 17:18:30 字数 555 浏览 1 评论 0原文

是否可以通过 ViewStub 将自定义属性传递到目标布局的根元素?像这样:

<ViewStub
        android:layout="@layout/custom_view"
        app:customAttr="12345"
        />

其中custom_view.xml 是:

<blah.CustomView ...>
    ...
</blah.CustomView>

当我尝试这样做时,CustomView.java 不会在AttributeSet 中获取“app:customAttr”。

当我直接使用 CustomView 时,不使用 ViewStub

<blah.Custom app:customAttr="12345"/>

属性进入 AttributeSet 就ok了。

但它不再偷懒了。

有什么解决办法吗?

谢谢你, 尤里。

Is it possible to pass custom attributes through a ViewStub to the target layout's root element? Like so:

<ViewStub
        android:layout="@layout/custom_view"
        app:customAttr="12345"
        />

Where custom_view.xml is:

<blah.CustomView ...>
    ...
</blah.CustomView>

When I try to do that, CustomView.java does not get "app:customAttr" in the AttributeSet.

When I use CustomView directly, without ViewStub

<blah.Custom app:customAttr="12345"/>

The attribute gets into the AttributeSet ok.

But it's not lazy anymore.

Any solutions?

Thank you,
Yuri.

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

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

发布评论

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

评论(1

哽咽笑 2024-09-13 17:18:30

一样,ViewStub 允许您覆盖的唯一属性是布局属性以及子视图在膨胀后将具有的 id。

Like <include> the only attributes that ViewStub lets you override are the layout attributes and which id the child view will have after inflation.

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