【发布时间】:2020-12-26 13:11:38
【问题描述】:
在我的 Android 应用程序中,我有一个带有 SwitchCompat 的列表来过滤列表数据。默认主题不能满足我的目的,我需要的是一个类似 iOS 的开关。我不知道如何自定义它以使其看起来与 iOS 开关完全一样。感谢阅读。
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@xml/custom_switch_background_main"
android:theme="@style/AppTheme.SwitchOverlay"/>
这是我目前存档的内容
custom_switch_background_main.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@xml/custom_switch_background" />
<item android:state_checked="true" android:drawable="@xml/custom_switch_background" />
</selector>
custom_switch_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50dp" />
<solid android:color="@color/transparent" />
<stroke
android:width="1dp"
android:color="@color/hello" />
</shape>
这就是我想要的
【问题讨论】:
-
material.io/develop/android/components/buttons的 MaterialButtonToggleGroup 怎么样
-
哇,很接近,不过是一个问题。我不想要传统的
Button。如何自定义那些内部按钮? -
查看同一链接中的主题按钮。
标签: android android-layout android-button material-components-android android-switch