自定义参数中,设置数组内容,数组值,数组文字颜色,数组背景颜色,数组icon id
<包名.CommonLinearTextLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_13"
app:common_linear_state="@{item.status}"
app:common_linear_state_array_attr="@array/business_state"
app:common_linear_state_array_value="@array/business_state_value"
app:common_linear_state_array_text_color="@array/business_state_textColor"
app:common_linear_state_array_bg_color="@array/business_state_bgColor"
app:common_linear_state_array_icon_id="@array/business_state_icon"
app:common_linear_title_text="状态:"
app:common_linear_title_text_minWidth="@dimen/margin_100" />
res values目录下新增文件arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="business_state">
<item>已锁定</item>
<item>待审核</item>
<item>正常</item>
</string-array>
<integer-array name="business_state_value">
<item>-1</item>
<item>0</item>
<item>1</item>
</integer-array>
<string-array name="business_state_textColor">
<item>common_color_5b6068</item>
<item>common_color_E95E13</item>
<item>common_color_67C23A</item>
</string-array>
<string-array name="business_state_bgColor">
<item>common_color_10_5b6068</item>
<item>common_color_10_E95E13</item>
<item>common_color_10_67C23A</item>
</string-array>
<string-array name="business_state_icon">
<item>icon_locked</item>
<item>icon_audit</item>
<item>icon_normal</item>
</string-array>
</resources>
注意:这里的颜色和图片,直接使用string-array,并且item定义的值,为文件名,或者颜色定义的名称
如何获取数组值
private int[] initColorOrDrawable(int arrayIconIdArray, int defaultId, String defType) {
String[] icons = getResources().getStringArray(arrayIconIdArray);
int[] idArrays = new int[icons.length];
for (int i = 0; i < icons.length; i++) {
int resId = getResources().getIdentifier(icons[i], defType, getPackageName());
if (resId != 0) {
idArrays[i] = resId;
} else {
idArrays[i] = defaultId;
}
}
return idArrays;
}
//获取字体颜色
int arrayColorResourceId = typedArray.getResourceId(R.styleable.common_linear_styleable_common_linear_state_array_text_color, 0);
if (arrayColorResourceId != 0) {
stateTextColorIdArray = initColorOrDrawable(arrayColorResourceId, R.color.common_color_67C23A, "color");
} else {
// 如果属性没有设置,设置默认值
stateTextColorIdArray = new int[]{R.color.common_color_67C23A, R.color.common_color_FB5352};
}
//获取图片
int arrayIconIdArray = typedArray.getResourceId(R.styleable.common_linear_styleable_common_linear_state_array_icon_id, 0);
if (arrayIconIdArray != 0) {
stateIconIdArray = initColorOrDrawable(arrayIconIdArray, R.drawable.icon_close, "drawable");
} else {
// 如果属性没有设置,设置默认值
stateIconIdArray = new int[]{R.drawable.icon_normal, R.drawable.icon_close};
}
//获取Drawable
private Drawable getImg(int state) {
for (int i = 0; i < stateArray.size(); i++) {
if (stateArray.get(i) == state) {
if (i < imgIdArray.size()) {
return getResources().getDrawable(imgIdArray.get(i));
}
break;
}
}
if (defaultImgId != 0) {
return getResources().getDrawable(defaultImgId);
}
return new ColorDrawable(Color.TRANSPARENT);
}
//设置图片显示
Drawable img = tv.getImg(stateValue);
tv.setImage(img);