微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwitchCompat RTL支持

如何解决SwitchCompat RTL支持

似乎track属性的选择器文件无法在RTL中正常工作。

LTR trackthumb

enter image description here

enter image description here

RTL trackthumbthumb此处已正确更改,但track图像仍未更改,因为它应在右侧显示地图图标。

enter image description here

<androidx.appcompat.widget.SwitchCompat
        android:background="@drawable/selector_switch_track"
        android:elevation="6dp"
        android:outlineProvider="background"
        android:thumb="@drawable/selector_switch_thumb"
        app:track="@drawable/selector_switch_track" />

selector_switch_track.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true">
    <item android:state_checked="true">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/global_white" />
                    <corners android:radius="@dimen/margin_100dp" />
                </shape>
            </item>
            <item android:drawable="@drawable/layer_switch_thumb_map_off" android:gravity="start" android:right="@dimen/margin_56dp" />
            <item android:drawable="@drawable/layer_switch_thumb_list_off" android:gravity="end" android:left="@dimen/margin_56dp" />
        </layer-list>
    </item>
    <item android:state_checked="false">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/global_white" />
                    <corners android:radius="@dimen/margin_100dp" />
                </shape>
            </item>
            <item android:drawable="@drawable/layer_switch_thumb_map_off" android:gravity="start" android:right="@dimen/margin_56dp" />
            <item android:drawable="@drawable/layer_switch_thumb_list_off" android:gravity="end" android:left="@dimen/margin_56dp" />
        </layer-list>
    </item>
</selector>

解决方法

我通过为RTL创建单独的res dir drawable-ldrtl并放置相同的selector文件,但切换了开始/结束可绘制对象的方式解决了这个问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。