在我的安卓应用程序中,我有一个带有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>这就是我想要的

发布于 2021-02-07 11:26:30
我通过使用一个自定义向量(里面的文本是一个掩码)来实现这个设计,它取代了开关的状态指示符。然后,我用一个定制的边框设计改变了开关压缩背景向量,以实现边框。我很快就会再次更新这个答案。
发布于 2020-09-10 10:21:03
你可以实现这样的目标:

使用标准的MaterialButtonToggleGroup
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:selectionRequired="true"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.rounded"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ITEM1"/>
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.rounded"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ITEM2"/>
</com.google.android.material.button.MaterialButtonToggleGroup>通过以下方式:
<style name="ShapeAppearanceOverlay.App.rounded" parent="">
<item name="cornerSize">50%</item>
</style>如果需要边框,可以将MaterialButtonToggleGroup包装在容器中(LinearLayout,CardView.)划一小块垫子。
如果您希望按钮两边都是圆角,您可以检查类似于这个问题的内容。
发布于 2020-09-08 08:29:48
https://stackoverflow.com/questions/63788998
复制相似问题