android中的数据绑定,Android中的静态数据绑定[literals]

我创建了包含图像和标题的自定义布局.要重复使用此布局,我正在使用< 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值