【问题标题】:Customise Android SwitchCompat自定义 Android SwitchCompat
【发布时间】: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


【解决方案1】:

我认为您应该使用滑动标签布局而不是 switchcompat 来满足您的要求。我找到了一个第三方库,看起来和你想要的一样。

Click Here to Use This Library

【讨论】:

  • 不,如果没有标签,我会告诉你。
  • 好的,但我还是想自定义SwitchCompat
【解决方案2】:

您可以实现以下目标:

使用标准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 包裹在一个容器中(LinearLayoutCardView....)应用一个小填充的描边。

如果你想要两边都有圆角的按钮,你可以检查this question之类的东西。

【讨论】:

  • 嘿,谢谢你的回答,我已经实现了我所说的设计。我稍后会发布。
  • 尽快发帖,对我们有帮助。
【解决方案3】:

我通过使用自定义矢量(里面的文本是一个掩码)来替代开关的状态指示器来实现设计。然后我用自定义边框设计更改了开关兼容背景矢量以实现边框。我很快会再次更新这个答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多