Android中的自定义SwitchComapt

问题描述

enter image description here

我现在的任务是像这样自定义switchCompat

所以这是开关兼容性本身

 <androidx.appcompat.widget.SwitchCompat
            android:id="@+id/availability_switch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:textAppearance="@style/Caption"
            android:textOff="@string/no"
            android:textOn="@string/yes"
            android:textStyle="bold"
            android:thumb="@drawable/switch_button_background"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="@+id/availableStatusTextView"
            app:layout_constraintTop_toTopOf="@+id/availableStatusTextView"
            app:showtext="true"
            app:track="@drawable/available_track"/>

这是我的自定义曲目

<?xml version="1.0" encoding="utf-8"?>
<item android:state_checked="false">

    <shape android:shape="rectangle">
        <padding android:top="4dp"
            android:bottom="4dp"/>
        <size
            android:width="64dp"
            android:height="16dp" />
            <solid android:color="@android:color/white"/>
            <corners android:radius="100sp"/>
            <stroke
                android:color="@color/past_red_color"
                android:width="1dp"/>
    </shape>
</item>


    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <padding android:top="4dp"
                android:bottom="4dp"/>
            <size
                android:width="64dp"
                android:height="16dp" />
            <solid android:color="@color/white"/>
            <corners android:radius="100sp"/>
            <stroke
                android:color="@color/past_green_color"
                android:width="1dp"/>
        </shape>
    </item>

这是我的自定义开关

<item
    android:state_checked="false">
    <shape android:shape="oval"
        android:dither="true"
        android:useLevel="true"
        android:visible="true">
        <solid android:color="#ffffff"/>
            <size
                android:width="32dp"
                android:height="32dp"/>
        <stroke android:width="1dp"
            android:color="#f98c98"/>
    </shape>
</item>

<item android:state_checked="true">
    <shape android:shape="oval"
        android:dither="true"
        android:useLevel="true"
        android:visible="true">
        <solid android:color="#ffffff"/>
        <size android:width="32dp"
            android:height="32dp"/>
        <stroke android:width="1dp"
            android:color="@color/past_green_color"/>
    </shape>
</item>

但这就是它的样子

所以这里我有问题:

(1)尽管我在跟踪项目中添加了填充,但拇指不能大于轨道

(2)不能将颜色设置为文本,因此“否”为红色,“是”为绿色

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)