android - 如何在Android中更改开关的文本颜色

标签 android switch-statement android-4.0-ice-cream-sandwich

我正在创建一个使用 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);

...对于 setTextColorSwitch - 如果您的 SwitchTextAppearance 样式不提供 ,将使用此颜色>textColor

你可以在setSwitchTextAppearanceSwitch源码中查看:

    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/

相关文章:

android - 繁重布局的流畅动画

java - 制作一个创建对象并在单击时显示的按钮

android - 应用程序如何获取我的电话号码以查找 friend ?

C程序切换语句

c++ - 如何简洁地编写 'switch' 语句来猜测用户正在考虑的数字

java - 做while循环错误(switch语句)

android - 使用 MockContext 时 ApplicationTestCase.createApplication() 中的 AssertionFailedError 在较新的 Android 版本中

java - import dalvik.system.VMRuntime 找不到

java - Ice Cream Sandwich 无法识别 HTML 中的换行符

Android 4.0 与 4.2 API 差异