【问题标题】:how to add color to the circle in the switch in android?如何在android中的switch中为圆圈添加颜色?
【发布时间】:2017-05-06 15:53:16
【问题描述】:

我想创建一个类似的开关

我就是这样做的

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Available"
        android:textColor="#82BCB4"
        android:textSize="20sp"
        />
    <Switch
        android:id="@+id/theSwitchId"
        android:textOn=""
        android:textOff=""
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:thumbTextPadding="5dp"
        android:switchMinWidth="90dp"
        android:layout_marginStart="40dp"
        android:minHeight="60dp"
        android:layout_gravity="center|center_vertical" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UnAvailable"
        android:layout_marginStart="40dp"
        android:textColor="#224e6d"
        android:textSize="20sp"
        />
</LinearLayout>

我无法更改开关的高度,并且在此图像中看起来不怎么样。请帮我解决这个问题

【问题讨论】:

  • 您可以尝试使用此处描述的选择器:stackoverflow.com/a/11499595/1468093
  • 我对此有点困惑,对此完全陌生.. 是不是这样,当我点击开关时,切换开关从左向右移动,没有放置图像@KamranAhmed跨度>

标签: android android-layout android-switch


【解决方案1】:

使用以下方法更改拇指(开关上的圆圈)颜色:

android:thumbTint="@color/thumbColor"

在您的 colors.xml 中:

<color name="thumbColor">#FFFFFF</color>

【讨论】:

    【解决方案2】:

    您在可绘制的 xml 中创建 Thumb: 拇指.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="false" android:drawable="@drawable/ic_thumbselecter"/>
        <item android:drawable="@drawable/ic_thumbselecter"/>
    </selector>
    

    并放入开关(在我的情况下,我正在使用它):

    <Switch
        app:kswThumbDrawable="@drawable/thumb"/>
    

    【讨论】:

      【解决方案3】:

      您需要做的只是使用自定义可绘制对象作为 switch

      track

      这是自定义drawable的代码:

      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="false">
          <shape android:shape="rectangle">
              <corners
                  android:radius="@dimen/_40sdp" />
              <solid
                  android:color="@android:color/darker_gray" />
              <padding
                  android:bottom="@dimen/_5dp"
                  android:left="@dimen/_5dp"
                  android:right="@dimen/_5dp"
                  android:top="@dimen/_5dp" />
          </shape>
      </item>
      

      进一步对现有布局进行这些修改 添加 android:thumbTint="@android:color/holo_blue_dark" 或任何符合您要求的颜色

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Available"
          android:textColor="#82BCB4"
          android:textSize="20sp" />
      
      <Switch
          android:id="@+id/theSwitchId"
          android:layout_width="wrap_content"
          android:layout_height="20dp"
          android:layout_gravity="center|center_vertical"
          android:layout_marginStart="40dp"
          android:minHeight="60dp"
          android:switchMinWidth="90dp"
          android:textOff=""
          android:textOn=""
          android:thumbTextPadding="5dp"
          android:thumbTint="@android:color/holo_blue_dark"
          android:track="@drawable/track_background" />
      
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="40dp"
          android:text="UnAvailable"
          android:textColor="#224e6d"
          android:textSize="20sp" />
      

      希望对你有帮助

      单击此处查看生成的屏幕截图

      screenshot

      【讨论】:

      • 谢谢@Subir Chakraborty
      【解决方案4】:

      在 style.xml 中

         <style name="switchCompatStyle">
              <item name="colorControlActivated">@color/colorPrimary</item>
          </style>
      

      开关的代码是这样的

       <android.support.v7.widget.SwitchCompat
                      android:id="@+id/switch"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:padding="@dimen/_10sdp"
                      app:theme="@style/switchCompatStyle" />
      

      【讨论】:

      • 如果我添加 @Rishabh Mahatha,则开关本身不可见
      • 你是否在你的gradle文件中添加了依赖compile 'com.android.support:appcompat-v7:25.0.0'
      • 将构建版本更改为 17
      • 构建失败.. @Rishabh Mahatha
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 2016-09-10
      • 1970-01-01
      相关资源
      最近更新 更多