【发布时间】:2020-02-26 00:16:05
【问题描述】:
视图绑定随 v3.6 发布。
文档: https://developer.android.com/topic/libraries/view-binding
我的问题是,有人知道如何将视图绑定与包含的布局一起使用吗?
包含另一个布局的给定布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/my_header"
layout="@layout/item_header"
android:layout_width="match_parent"
android:layout_height="100dp" />
</LinearLayout>
我正在尝试引用 item_header 布局内的项目。
binder.my_header (<-- this just returns back the view)
binder.root (<-- this just returns back the root view)
即使我将 id 添加到 item_header 的根目录,例如 id="@+id/parent_id" 并尝试引用它,我也会收到空指针异常
binder.parentId (<-- I have access to views inside of the item_header, however, I receive exceptions. Says that "parentId" cannot be found)
如何引用布局,item_header?
【问题讨论】:
-
在我刚刚运行的实验中,
binder.my_header是ItemHeaderBinding,而不是View。我可以毫无问题地引用其中的小部件(例如,binder.my_header.foo)。 -
@CommonsWare 你这个人!我没有意识到我可以参考。这现在就像一个魅力。您介意发表您的评论作为答案吗?我会接受的。
-
这是一个重复的问题。在这里查看我的答案stackoverflow.com/a/60616894/8040697