【发布时间】:2017-05-23 21:18:48
【问题描述】:
我有一个包含图标和文本的按钮。
这是通过以下代码实现的:
<Button
...
android:drawableLeft="@drawable/ghana"
android:drawableStart="@drawable/ghana"
android:text="@string/hint_ghana"
/>
当我这样做时,数据绑定适用于文本,但不适用于图标:
<Button
...
android:drawableLeft="@{countryOfResidence.thumbnail}"
android:drawableStart="@{countryOfResidence.thumbnail}"
android:text="@{countryOfResidence.countryName}"
/>
这是结果:
我一直在寻找解决方案,但找不到任何解决方案,因为大多数人都专注于将图像加载到 ImageView 中。
我的模型类如下所示:
public class CountryOfResidence {
private String countryName;
private int thumbnail;
public CountryOfResidence(String countryName, int thumbnail) {
this.setCountryName(countryName);
this.setThumbnail(thumbnail);
}
....
在调用Activity的onCreate()方法中,正在这样做
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivitySignUpBinding signUpBinding = DataBindingUtil.setContentView(this, R.layout.activity_sign_up);
MyHandler myHandler = new MyHandler();
signUpBinding.setMyHandler(myHandler);
mViewFlipper = (ViewFlipper) findViewById(R.id.signUpViewFlipper);
countryOfResidenceList = new ArrayList<>();
countryOfResidenceList.add(new CountryOfResidence("Nigeria", R.drawable.nigeria));
countryOfResidenceList.add(new CountryOfResidence("Ghana", R.drawable.ghana));
countryOfResidenceList.add(new CountryOfResidence("Kenya", R.drawable.kenya));
signUpBinding.setCountryOfResidence(countryOfResidenceList.get(1));
最后,我的布局中的数据标签是这样的
<data>
...
<variable
name="countryOfResidence"
type="orem_tech.com.teylur.model.CountryOfResidence"/>
</data>
有什么建议吗?
【问题讨论】:
标签: android data-binding