【问题标题】:Android SwitchCompat styleAndroid SwitchCompat 风格
【发布时间】:2016-03-30 16:43:22
【问题描述】:

我在装有 android 5.1.1 的新设备上测试了我的应用。在我的 SettingsActivity 我有一个开关。我已经阅读了一些帖子并将其更改为android.support.v7.widget.SwitchCompat,但问题仍然如下:在我的旧设备上,开关看起来非常好。您可以为textOntextOff 设置两个文本,它非常适合。但自从 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>

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>

【问题讨论】:

  • 你能发布layout 和任何相关的style xmls 吗?
  • 我想我明白了。您知道如何更改 switchButton-text 的颜色吗?
  • 我更新了答案,为样式添加了文本颜色。

标签: android switch-statement styles switchcompat


【解决方案1】:

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

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

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

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

【讨论】:

  • 我使用了你的模式,但它似乎不起作用 android:switchTextAppearance="@style/SwitchTextAppearance"
  • 这不起作用,因为android:switchTextAppearance="@style/SwitchTextAppearance" 不是一个可识别的属性。您需要使用xmlns:app="http://schemas.android.com/apk/res-auto" 命名空间,所以它就像app:switchTextAppearance="@style/SwitchTextAppearance"
  • android:switchTextAppearance="@style/SwitchTextAppearance"更改为app:switchTextAppearance="@style/SwitchTextAppearance",它应该会改变开关内文本的颜色和大小。
猜你喜欢
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多