android的TextView数据绑定:drawableBottom
我正在为android的TextView数据绑定:drawableBottom,android,android-layout,android-databinding,Android,Android Layout,Android Databinding,我正在为TabLayout扩展布局,并在活动类中绑定数据。在这方面,我一直在使用android:drawableBottom内部TextView的数据绑定。我的布局代码如下: <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" > <data> <variable na
TabLayout
扩展布局,并在活动
类中绑定数据。在这方面,我一直在使用android:drawableBottom
内部TextView
的数据绑定。我的布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" >
<data>
<variable
name="item"
type="<package-name>.HomeTabItem"/>
</data>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxLines="1"
android:text="@={item.name}"
android:textColor="@color/white"
android:drawableBottom="<What I have to-do here>"
android:textSize="@dimen/dimen_18" />
</layout>
private void setTabsLayoutItems() {
String tabItems[] = getResources().getStringArray(R.array.home_tab_items);
TypedArray tabItemsDrawable = getResources().obtainTypedArray(R.array.home_tab_items_drawable);
for (int i = 0; i < tabItems.length; i++) {
CustomTabBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.custom_tab, tabLayout, false);
int id = tabItemsDrawable.getResourceId(i, -1);
HomeTabItem obj = new HomeTabItem();
obj.setName(tabItems[i]);
obj.setIcon(id);
binding.setItem(obj);
View cropsTab = binding.getRoot();
tabLayout.addTab(tabLayout.newTab().setCustomView(cropsTab));
}
tabItemsDrawable.recycle();
}
public class HomeTabItem extends BaseObservable {
private String name;
private int icon;
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(com.sdei.farmx.BR.name);
}
@Bindable
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
notifyPropertyChanged(com.sdei.farmx.BR.icon);
}
}
HomeTabItem
类如下:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" >
<data>
<variable
name="item"
type="<package-name>.HomeTabItem"/>
</data>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxLines="1"
android:text="@={item.name}"
android:textColor="@color/white"
android:drawableBottom="<What I have to-do here>"
android:textSize="@dimen/dimen_18" />
</layout>
private void setTabsLayoutItems() {
String tabItems[] = getResources().getStringArray(R.array.home_tab_items);
TypedArray tabItemsDrawable = getResources().obtainTypedArray(R.array.home_tab_items_drawable);
for (int i = 0; i < tabItems.length; i++) {
CustomTabBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.custom_tab, tabLayout, false);
int id = tabItemsDrawable.getResourceId(i, -1);
HomeTabItem obj = new HomeTabItem();
obj.setName(tabItems[i]);
obj.setIcon(id);
binding.setItem(obj);
View cropsTab = binding.getRoot();
tabLayout.addTab(tabLayout.newTab().setCustomView(cropsTab));
}
tabItemsDrawable.recycle();
}
public class HomeTabItem extends BaseObservable {
private String name;
private int icon;
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(com.sdei.farmx.BR.name);
}
@Bindable
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
notifyPropertyChanged(com.sdei.farmx.BR.icon);
}
}
尝试直接设置iconID,如下所示:
android:drawableBottom="@{item.icon}"
如果它不起作用,您必须创建自定义绑定来设置它。为此,创建名为Bindings的最终类,并添加以下方法:
@BindingAdapter({"icon"})
public static void icon(TextView view, int iconId) {
view.setCompoundDrawables(null,null,null, view.getContext().getDrawable(iconId));
}
并在布局中调用它:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxLines="1"
android:text="@={item.name}"
android:textColor="@color/white"
app:icon="@{item.icon}"
android:textSize="@dimen/dimen_18" />
</layout>
android:background=“@drawable/rounded\u corner\u search”最终课程?你能解释一下吗?这个名字只是一个简单的类