Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SwitchCompat风格_Android_Switch Statement_Styles_Switchcompat - Fatal编程技术网

Android SwitchCompat风格

Android SwitchCompat风格,android,switch-statement,styles,switchcompat,Android,Switch Statement,Styles,Switchcompat,我用安卓5.1.1在我的新设备上测试了我的应用程序。在我的设置活动中,我有一个开关。我已经阅读了一些帖子,并将其改为android.support.v7.widget.SwitchCompat,但问题仍然是:在我的旧设备上,开关看起来非常漂亮。您可以为textOn和textOff设置两个文本,这两个文本非常适合。但从api 21或其他我在这里看到的小混蛋开始: 看起来像****。如何为所有设备(如棒棒糖开关)重新设计它的样式 编辑: 以上问题已得到回答。 缺少一件小事:如何更改开关上文本的颜色

我用安卓5.1.1在我的新设备上测试了我的应用程序。在我的设置活动中,我有一个开关。我已经阅读了一些帖子,并将其改为
android.support.v7.widget.SwitchCompat
,但问题仍然是:在我的旧设备上,开关看起来非常漂亮。您可以为
textOn
textOff
设置两个文本,这两个文本非常适合。但从api 21或其他我在这里看到的小混蛋开始:

看起来像****。如何为所有设备(如棒棒糖开关)重新设计它的样式

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

styles.xml

<style name="SwitchTextAppearance" parent="TextAppearance.AppCompat.Widget.Switch">
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">#3F51B5</item>
    </style>
<?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>

12便士
#3F51B5
layout.xml

<style name="SwitchTextAppearance" parent="TextAppearance.AppCompat.Widget.Switch">
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">#3F51B5</item>
    </style>
<?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
可以设置显示在开关内部的文本样式

添加类似这样的样式,并使用
setSwitchTextAppearance
进行设置:

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

12便士
#3F51B5

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

您可以发布
布局
和任何相关的
样式
xmls吗?我想我知道了。你知道如何更改switchButton文本的颜色吗?我更新了答案,在样式中添加了文本颜色。我使用了你的模式,但它似乎不起作用android:switchTextAppearance=“@style/switchTextAppearance”因为
android:switchTextAppearance=“@style/switchTextAppearance”不起作用
不是可识别的属性。您需要使用
xmlns:app=”http://schemas.android.com/apk/res-auto“
namespace,因此它将类似于
app:switchTextAppearance=“@style/switchTextAppearance”
Change
android:switchTextAppearance=“@style/switchTextAppearance”
app:switchTextAppearance=“@style/switchTextAppearance”
并且它应该改变开关内文本的颜色和大小。