Android SwitchCompat 风格 [英] Android SwitchCompat style
问题描述
我在装有 android 5.1.1 的新设备上测试了我的应用.在我的 SettingsActivity 我有一个开关.我已经阅读了一些帖子并将其更改为 android.support.v7.widget.SwitchCompat
但问题仍然如下: 在我的旧设备上,开关看起来非常好.您可以为 textOn
和 textOff
设置两个文本,它非常适合.但自从 api 21 或任何我在这里得到这个小混蛋:
I tested my app on my new device with android 5.1.1. In my SettingsActivity i have a switch. I already read some posts and changed it to android.support.v7.widget.SwitchCompat
but the problem still is the following: On my old device the switch looks very nice. you can set two texts for textOn
and textOff
and it fits perfectly. but since api 21 or whatever I get this little bastard here:
看起来像****.如何为棒棒糖开关等所有设备重新设置样式?
It looks like ****. How can I restyle it for all devices like the lollypop switch?
上面的问题得到了解答.缺少一件小事:如何更改开关上文本的颜色.(不是左侧的标签!!!)
The question above is answered. One tiny thing is missing: How to change the color of the text on the switch.(Not the tag on the left side!!!)
styles.xml
<style name="SwitchTextAppearance" parent="TextAppearance.AppCompat.Widget.Switch">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#3F51B5</item>
</style>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="12dp"
android:paddingTop="12dp">
<android.support.v7.widget.SwitchCompat
android:id="@+id/mySwitch"
android:layout_width="match_parent"
android:switchMinWidth="56dp"
android:layout_height="wrap_content"
android:switchTextAppearance="@style/SwitchTextAppearance"
android:thumb="@drawable/thumb"
android:track="@drawable/track"
app:showText="true"
android:textOn="ON"
android:textOff="OFF"
android:text="Toggle Switch"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="@android:color/transparent"
android:button="@null"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
推荐答案
使用 SwitchCompat.setSwitchTextAppearance
您可以设置显示在开关内部的文本的样式.
Using SwitchCompat.setSwitchTextAppearance
you can set the style for the text that appears inside the switch itself.
添加类似这样的样式并使用 setSwitchTextAppearance
设置它:
Add a style something like this and set it using setSwitchTextAppearance
:
<style name="SwitchTextAppearance" parent="TextAppearance.AppCompat.Widget.Switch">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#3F51B5</item>
</style>
并且您应该能够自定义OFF"文本的大小、颜色等.
And you should be able to customize the size, color, etc of your "OFF" text.
这篇关于Android SwitchCompat 风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!