自定义形状开关是这样的:
API 21 以上
低于 API 21
好像是 <size/>
block 在 <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"/>
drawable/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>
drawable/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>
也许有人遇到过类似的问题。请分享你的经验。
编辑:添加使用的尺寸
<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>
最佳答案
<size />
一切正常标签。 Drawable
被正确创建和应用。您的问题完全在于 Switch
.
在之前的 Lollipop 版本中,拇指与文本一起使用,而可绘制对象只不过是一个背景图像,它被缩放到必要的大小。您可以通过向 textOff
添加文本来验证这一点。和 textOn
属性。此外,还定义了一个最小宽度。
所以只需添加 switchMinWidth
0 和 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" />
以及正确的半径定义
<dimen name="switch_track_height">30dp</dimen>
<dimen name="switch_thumb_radius">15dp</dimen>
关于android - 自定义开关 - 轨道和选择器大小在 21 API 以下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39206494/