首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >定制Android SwitchCompat

定制Android SwitchCompat
EN

Stack Overflow用户
提问于 2020-09-08 07:23:51
回答 3查看 998关注 0票数 1

在我的安卓应用程序中,我有一个带有SwitchCompat的列表来过滤列表数据。默认的主题不能满足我的目的,我需要的是一个类似于iOS的开关。我不知道如何定制它,使其看起来与iOS开关完全一样。感谢您的阅读。

代码语言:javascript
复制
            <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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>

这就是我想要的

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-02-07 11:26:30

我通过使用一个自定义向量(里面的文本是一个掩码)来实现这个设计,它取代了开关的状态指示符。然后,我用一个定制的边框设计改变了开关压缩背景向量,以实现边框。我很快就会再次更新这个答案。

票数 0
EN

Stack Overflow用户

发布于 2020-09-10 10:21:03

你可以实现这样的目标:

使用标准的MaterialButtonToggleGroup

代码语言:javascript
复制
      <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>

通过以下方式:

代码语言:javascript
复制
<style name="ShapeAppearanceOverlay.App.rounded" parent="">
    <item name="cornerSize">50%</item>
</style>

如果需要边框,可以将MaterialButtonToggleGroup包装在容器中(LinearLayoutCardView.)划一小块垫子。

如果您希望按钮两边都是圆角,您可以检查类似于这个问题的内容。

票数 1
EN

Stack Overflow用户

发布于 2020-09-08 08:29:48

我认为您应该使用滑动选项卡布局,而不是开关,以满足您的要求。我找到了一个第三方库,它看起来像你想要的一样的开关。

单击此处使用此库

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63788998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档