View Binding于v3.6发布。
文档: https://developer.android.com/topic/libraries/view-binding
我的问题是,有人知道如何在包含布局中使用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)
即使我给item_header的根添加一个id,比如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