How to apply a custom style to SwitchCompat

放肆的年华 提交于 2020-01-14 06:58:15

问题


I would like to apply a custom style to SwitchCompat. Change drawables and text for on and off state. How can I achieve this? I can't find any examples on how this is done. I tried the following in my styles.xml but apparently I'm not using the right parent:

<style name="Switch" parent="android:Widget.AppCompat.Widget.CompoundButton.SwitchCompat">
    <item name="android:textOn">@string/common_yes</item>
    <item name="android:textOff">@string/common_no</item>
    <item name="android:thumb">@drawable/btn_switch_selector</item>
    <item name="android:track">@drawable/btn_switch_bg_selector</item>
</style>

Edit

I managed to change the drawables in code.

switchView.setThumbResource(R.drawable.btn_switch_selector);
switchView.setTrackResource(R.drawable.btn_switch_bg_selector);

but I haven't found a way yet to change the text of the switch. The following snippet doesn't seem to work. Maybe I need to set some more text-properties?

switchView.setTextOn(context.getString(R.string.common_yes));
switchView.setTextOff(context.getString(R.string.common_no));

According to the SwitchCompat source code there should be support for on/off text: https://android.googlesource.com/platform/frameworks/support/+/421d8baa4a524e1384bcf033360bccaf8d55081d/v7/appcompat/src/android/support/v7/widget/SwitchCompat.java

The {@link #setText(CharSequence) text} property controls the text displayed in the label for the switch, whereas the {@link #setTextOff(CharSequence) off} and {@link #setTextOn(CharSequence) on} text controls the text on the thumb.

Edit 2

Finally found a code solution. Apparently setShowText() needs to be set to true for the text to appear on the switch.

switchView.setTextOn(context.getString(R.string.common_yes));
switchView.setTextOff(context.getString(R.string.common_no));
switchView.setShowText(true);
switchView.setThumbResource(R.drawable.btn_switch_selector);
switchView.setTrackResource(R.drawable.btn_switch_bg_selector);

and xml solution

<android.support.v7.widget.SwitchCompat
        android:id="@+id/view_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"            
        android:thumb="@drawable/btn_switch_selector"
        app:track="@drawable/btn_switch_bg_selector"
        android:textOn="@string/common_yes"
        android:textOff="@string/common_no"
        app:showText="true" />

I'd still like to know if there is a way to put this in styles.xml.


回答1:


Finally I found a way to apply the same style throughout my whole app. I put the following line in my themes.xml

<item name="switchStyle">@style/Custom.Widget.SwitchCompat</item>

And the following in my styles.xml

<style name="Custom.Widget.SwitchCompat" >
    <item name="android:thumb">@drawable/btn_switch_selector</item>
    <item name="track">@drawable/btn_switch_bg_selector</item>
    <item name="showText">true</item>
    <item name="android:textOn">@string/common_yes</item>
    <item name="android:textOff">@string/common_no</item>
</style>



回答2:


The style should include the parent

<item name="switchStyle">@style/MySwitchCompat</item>

<style name="MySwitchCompat" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="android:textColor">@color/accent</item>
    <item name="android:fontFamily">sans-serif-light</item>
</style>


来源:https://stackoverflow.com/questions/27562050/how-to-apply-a-custom-style-to-switchcompat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!