自定义开关 - 轨道和选择器尺寸在21 API以下不能工作

AnZ*_*AnZ 19 xml android android-switch

自定义形状开关看起来像这样:

以上API 21

在此输入图像描述

API 21以下

在此输入图像描述

看起来像<size/>块不适<shape/>用于21之前的API.

任何想法如何解决这个问题?


container.xml中:

<Switch
        android:id="@id/switch_follow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:textOff=""
        android:textOn=""
        android:thumb="@drawable/switch_selector"
        android:track="@drawable/switch_track"/>
Run Code Online (Sandbox Code Playgroud)

绘制/ switch_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <layer-list>
        <item
            android:bottom="@dimen/switch_selector_padding"
            android:left="@dimen/switch_selector_padding"
            android:right="@dimen/switch_selector_padding"
            android:top="@dimen/switch_selector_padding">
            <shape
                android:dither="true"
                android:shape="oval"
                android:useLevel="false"
                android:visible="true">
                <gradient
                    android:angle="270"
                    android:endColor="@color/primary_white"
                    android:startColor="@color/primary_white"/>
                <corners
                    android:radius="@dimen/switch_radius"/>
                <size
                    android:width="@dimen/switch_track_height"
                    android:height="@dimen/switch_track_height" />
            </shape>
        </item>

    </layer-list>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)

绘制/ switch_track.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="rectangle"
android:useLevel="false"
android:visible="true">
<gradient
    android:angle="270"
    android:endColor="@color/primary_yellow_dark_v2"
    android:startColor="@color/primary_yellow_dark_v2"/>
<corners android:radius="@dimen/switch_radius" />
<stroke
    android:width="@dimen/switch_stroke_height"
    android:color="@android:color/transparent">
</stroke>
<size
    android:width="@dimen/switch_track_width"
    android:height="@dimen/switch_track_height" />
</shape>
Run Code Online (Sandbox Code Playgroud)

也许有人遇到了类似的问题.请分享您的经验.


编辑:添加使用的尺寸

<dimen name="switch_track_width">36dp</dimen>
<dimen name="switch_track_height">30dp</dimen>
<dimen name="switch_radius">50dp</dimen>
<dimen name="switch_selector_padding">2dp</dimen>
<dimen name="switch_stroke_height">0dp</dimen>
Run Code Online (Sandbox Code Playgroud)

tyn*_*ynn 11

<size />标签一切都好.的Drawable创建和正确应用.你的问题完全在于Switch.

在旧版本中,之前的Lollipop拇指与文本一起使用,而drawable只不过是一个背景图像,它被缩放到必要的大小.您可以通过向textOfftextOn属性添加文本来验证这一点.此外,还定义了最小宽度.

所以只需添加switchMinWidth0和thumbTextPadding拇指直径的一半即可

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:switchMinWidth="0dp"
    android:textOff=""
    android:textOn=""
    android:thumb="@drawable/switch_selector"
    android:thumbTextPadding="@dimen/switch_thumb_radius"
    android:track="@drawable/switch_track" />
Run Code Online (Sandbox Code Playgroud)

和正确的半径定义

<dimen name="switch_track_height">30dp</dimen>
<dimen name="switch_thumb_radius">15dp</dimen>
Run Code Online (Sandbox Code Playgroud)