如何申请自定义样式SwitchCompat [英] How to apply a custom style to SwitchCompat
问题描述
我想自定义样式应用到SwitchCompat。更改可绘和文字的开启和关闭状态。我怎样才能做到这一点?我无法找到如何做到这一点的例子。我想在我的styles.xml以下,但显然我没有使用正确的父:
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>
修改
我设法改变code中的可绘制。
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));
据该SwitchCompat源$ C $ C应该有开/关文本支持:
<一href=\"https://android.googlesource.com/platform/frameworks/support/+/421d8baa4a524e1384bcf033360bccaf8d55081d/v7/appcompat/src/android/support/v7/widget/SwitchCompat.java\">https://android.googlesource.com/platform/frameworks/support/+/421d8baa4a524e1384bcf033360bccaf8d55081d/v7/appcompat/src/android/support/v7/widget/SwitchCompat.java
而{@link #setText(CharSequence的)文本}
属性控制在标签交换机显示的文本,而
{@link #setTextOff(CharSequence的)关}和{上@link #setTextOn(CharSequence的)}文本
控制拇指上的文字。
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.
编辑2
终于找到了code解决方案。显然 setShowText()
必须设置为真正
为出现在交换机上的文字。
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);
和XML解决方案
<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" />
我仍想知道是否有把它放到 styles.xml
办法。
推荐答案
最后,我找到了一种适用于整个我的整个应用程序相同的风格。
我把下面的一行在我的的themes.xml
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>
和在下面我 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>
这篇关于如何申请自定义样式SwitchCompat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!