我正在创建一个使用 Android 4.0 的应用程序。 我想知道是否可以在开关中更改文本的文本颜色。
我试过设置文字颜色,但不起作用。
有什么想法吗?
提前致谢!
最佳答案
你必须使用 android:switchTextAppearance
属性,例如:
android:switchTextAppearance="@style/SwitchTextAppearance"
风格方面:
<style name="SwitchTextAppearance" parent="@android:style/TextAppearance.Holo.Small">
<item name="android:textColor">@color/my_switch_color</item>
</style>
你也可以在代码中做,同样使用上面的样式:
mySwitch.setSwitchTextAppearance(getActivity(), R.style.SwitchTextAppearance);
...对于 setTextColor
和 Switch
- 如果您的 SwitchTextAppearance
样式不提供 ,将使用此颜色>textColor
你可以在setSwitchTextAppearance
的Switch
源码中查看:
ColorStateList colors;
int ts;
colors = appearance.getColorStateList(com.android.internal.R.styleable.
TextAppearance_textColor);
if (colors != null) {
mTextColors = colors;
} else {
// If no color set in TextAppearance, default to the view's textColor
mTextColors = getTextColors();
}
ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable.
TextAppearance_textSize, 0);
if (ts != 0) {
if (ts != mTextPaint.getTextSize()) {
mTextPaint.setTextSize(ts);
requestLayout();
}
}
关于android - 如何在Android中更改开关的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12706874/