如何解决Android中的自定义SwitchComapt
我现在的任务是像这样自定义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 举报,一经查实,本站将立刻删除。