Android数据绑定使用包含标签 [英] Android Data Binding using include tag
问题描述
更新:
上述示例正常工作,因为版本1.0-rc4固定需要不必要的变量的问题。 >
main.xml:
< layout xmlns:andr ...
< data>
< / data>
< include layout =@ layout / buttons>< / include>
....
buttons.xml:
< layout xmlns:andr ...>
< data>
< / data>
< Button
android:id =@ + id / button
..../>
MyActivity.java:
... binding = DataBindingUtil.inflate。
binding.button; - >无法解析符号'按钮'
如何获取按钮?
问题是包含的布局不被认为是数据绑定布局它作为一个,你需要传递一个变量:
buttons.xml:
code>< layout xmlns:andr ...>
< data>
< variable name =footype =int/>
& / data>
< Button
android:id =@ + id / button
..../>
main.xml:
< layout xmlns:andr ...
...
< include layout =@ layout / buttons
android:id =@ + id / buttons
app:foo =@ {1}/>
....
然后您可以通过按钮字段间接访问按钮: mainBinding绑定= MainBinding.inflate(getLayoutInflater()); p>
binding.buttons.button
从1.0-rc4(刚刚发布)开始,您不再需要该变量。您可以简化为:
buttons.xml:
; layout xmlns:andr ...>
< Button
android:id =@ + id / button
..../>
main.xml:
< layout xmlns:andr ...
...
< include layout =@ layout / buttons
android:id =@ + id / buttons/>
....
UPDATE:
The above example works properly, because release 1.0-rc4 fixed the issue of needing the unnecessary variable.
main.xml:
<layout xmlns:andr...
<data>
</data>
<include layout="@layout/buttons"></include>
....
buttons.xml:
<layout xmlns:andr...>
<data>
</data>
<Button
android:id="@+id/button"
...." />
MyActivity.java:
... binding = DataBindingUtil.inflate...
binding.button; ->cannot resolve symbol 'button'
how to get button?
The problem is that the included layout isn't being thought of as a data-bound layout. To make it act as one, you need to pass a variable:
buttons.xml:
<layout xmlns:andr...>
<data>
<variable name="foo" type="int"/>
</data>
<Button
android:id="@+id/button"
...." />
main.xml:
<layout xmlns:andr...
...
<include layout="@layout/buttons"
android:id="@+id/buttons"
app:foo="@{1}"/>
....
Then you can access buttons indirectly through the buttons field:
MainBinding binding = MainBinding.inflate(getLayoutInflater());
binding.buttons.button
As of 1.0-rc4 (just released), you no longer need the variable. You can simplify it to:
buttons.xml:
<layout xmlns:andr...>
<Button
android:id="@+id/button"
...." />
main.xml:
<layout xmlns:andr...
...
<include layout="@layout/buttons"
android:id="@+id/buttons"/>
....
这篇关于Android数据绑定使用包含标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!