【发布时间】:2014-05-06 19:46:10
【问题描述】:
在自定义视图组中,我有一个 TextView 作为孩子。我想根据 android:textColor 值设置这个 TextView 的 textColor。所以在 res/values/styles.xml 我有:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomViewGroupTextView">
<attr name="android:textColor" />
</declare-styleable>
</resources>
在 CustomViewGroup 的构造函数中,我有这个:
private TextView mTextView;
public CustomViewGroup(Context context) {
super(context);
initTextView(context, attrs);
}
public CustomViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
initTextView(context, attrs);
}
public CustomViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initTextView(context, attrs);
}
private void initTextView(Context context, AttributeSet attrs) {
mTextView = new TextView(
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomViewGroupTextView);
// Set text color
ColorStateList textColor = ta.getColorStateList(R.styleable.MinutiaeTextView_android_textColor);
if (textColor != null) {
mTextView.setTextColor(textColor);
}
}
我的问题是:如何正确执行 mTextView.setTextColor?任何人都可以将整个颜色状态列表或单个颜色值放入 android:textColor。或者,如果有人在 android:textColor 中输入一种颜色,我会得到一个颜色相同的 ColorStateList 吗?
【问题讨论】:
标签: android textview android-custom-view textcolor