我创建了包含图像和标题的自定义布局.要重复使用此布局,我正在使用< include>标签.问题是我甚至无法将字符串文字绑定到包含的布局中.我试图遵循这些instructions,但没有成功.
布局/ titlebar.xml
布局/ otherlayout.xml
xmlns:bind="http://schemas.android.com/apk/res-auto"
...
>
bind:title="@{Example}"
/>
...
在gradle中,我为模块启用了数据绑定:
android {
...
dataBinding {
enabled = true
}
...
}
解决方法:
修复了基于@CzarMatt答案的layout / otherlayout.xml
xmlns:bind="http://schemas.android.com/apk/res-auto">
bind:title='@{"Settings"}'
/>
...
数据绑定需要通过DataBindingUtil设置布局,如@RaviRupareliya建议的那样,否则数据绑定将不起作用:
public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.otherlayout);
DataBindingUtil.setContentView(this, R.layout.otherlayout);
}
...
}
标签:android,android-databinding
来源: https://codeday.me/bug/20190608/1196732.html