簡単な完全な例
id含まれるレイアウトに設定して、を使用するだけbinding.includedLayout.anyViewです。
この例は<include、コードに含まれるビューに値を渡し、それに含まれるビューにアクセスするのに役立ちます。
ステップ1
あなたが持っているlayout_common.xml、合格したいString含まれているレイアウト。
Stringレイアウトに変数を作成し、これStringをに参照しTextViewます。
<data>
// declare fields
<variable
name="passedText"
type="String"/>
</data>
<TextView
android:id="@+id/textView"
...
android:text="@{passedText}"/> //set field to your view.
ステップ2
このレイアウトを親レイアウトに含めます。組み込みidクラスにtoを指定して、バインディングクラスで使用できるようにします。これでpassedText、<includeタグに文字列を渡すことができます。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
..
>
<include
android:id="@+id/includedLayout"
layout="@layout/layout_common"
app:passedText="@{@string/app_name}" // here we pass any String
/>
</LinearLayout>
</layout>
binding.includedLayout.textViewクラスで使用できます。
上記のように、含まれるレイアウトに任意の変数を渡すことができます。
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.includedLayout.textView.setText("text");
注両方のレイアウト(親とインクルード)はbinding layout、<layout
<include layout="@layout/buttons" android:id="@+id/buttons"/>。ボタンビューにアクセスできるようにパブリックフィールドを生成するために、IDはまだ必要です。