如何更改开关的文本颜色在Android中 [英] How to change textcolor of switch in Android

查看:321
本文介绍了如何更改开关的文本颜色在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建它采用Android 4.0的应用程序。 我不知道是否有可能改变文本的文本颜色的开关。

I'm creating an application which uses Android 4.0. I'm wondering if it is possible to change the text color of the text in a switch.

我已经尝试设置文本的颜色,但它不能正常工作。

I've tried setting the text color, but it doesn't work.

任何想法?

在此先感谢!

推荐答案

您必须使用安卓switchTextAppearance 属性,如:

android:switchTextAppearance="@style/SwitchTextAppearance"

而在样式:

<style name="SwitchTextAppearance" parent="@android:style/TextAppearance.Holo.Small">
    <item name="android:textColor">@color/my_switch_color</item>
</style>

您也可以做到这一点在code,还用上面的样式:

you can also do it in code, also using above styles:

mySwitch.setSwitchTextAppearance(getActivity(), R.style.SwitchTextAppearance);


......并以此为 setTextColor 开关 - 这种颜色会如果你的<$ C $使用C> SwitchTextAppearance 风格不提供文字颜色


...and as for setTextColor and Switch - this color will be used if your SwitchTextAppearance style doesn't provide a textColor

您可以检查它在开关源$ C ​​$ c。在 setSwitchTextAppearance

you can check it in Switch source code in setSwitchTextAppearance:

    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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆