3

我用 android 5.1.1 在我的新设备上测试了我的应用程序。在我的 SettingsActivity 中,我有一个开关。我已经阅读了一些帖子并将其更改为,android.support.v7.widget.SwitchCompat但问题仍然如下:在我的旧设备上,开关看起来非常好。textOn您可以为and设置两个文本textOff,它非常适合。但是自从 api 21 或者我在这里得到这个小混蛋之后:

在此处输入图像描述

看起来像 ****。如何为棒棒糖开关等所有设备重新设计它?

编辑: 上面的问题已经回答。缺少一件小事:如何更改开关上文本的颜色。(不是左侧的标签!!!)

样式.xml

<style name="SwitchTextAppearance" parent="TextAppearance.AppCompat.Widget.Switch">
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">#3F51B5</item>
    </style>

布局.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>
4

1 回答 1

4

使用SwitchCompat.setSwitchTextAppearance您可以设置出现在开关本身内部的文本的样式。

添加类似这样的样式并使用setSwitchTextAppearance

<style name="SwitchTextAppearance" parent="TextAppearance.AppCompat.Widget.Switch">
  <item name="android:textSize">12sp</item>
  <item name="android:textColor">#3F51B5</item>
</style>

您应该能够自定义“关闭”文本的大小、颜色等。

于 2016-03-30T17:52:39.903 回答