自定义开关 - 在21 API以下,跟踪器和选择器大小不起作用

21

自定义形状开关如下:

API 21及以上

enter image description here

API 21以下

enter image description here

似乎在 API 21 以下的 <shape/> 中,<size/> 块不起作用。

有什么解决方法吗?


代码

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"/>

可绘制/开关选择器.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>

可绘制/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>

请发布@dimen/switch_track_width和height尺寸。 - Warpzit
@Warpzit,请检查我的编辑。 - AnZ
我认为(但不确定)这里的XML标签是错误的。除非它是由于XML语法错误引起的(似乎不是,特别是如果在以前的版本中工作过,但这并非不可能...),XML恰好是用户用于数据文件的格式,就像纯文本或“.ini”一样,这种格式在太多问题中被使用,实际上与此无关。 - bitifet
如果我是你,我会通过代码来改变它,你对此没问题吧? - Nanoc
1
我在自定义开关控件方面有很多工作要做,最终我使用了这个库完成了它,这个库有很大的潜力,你可以看看:https://github.com/kyleduo/SwitchButton - crgarridos
显示剩余3条评论
3个回答

12

<size />标签一切正常。 Drawable已正确创建并应用。您的问题完全在于Switch中。

在旧版本中,在Lollipop之前,滑块与文本一起使用,而Drawable只是一个背景图像,会按比例缩放到所需大小。您可以通过将文本添加到textOfftextOn属性来验证此操作。此外,有一个定义了最小宽度。

因此,只需添加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>

非常好的解释,谢谢!看起来它解决了我的问题。 - AnZ

1
我已经复制了你的代码并尝试在我的机器上实现,首先,在你的drawable/switch_selector.xml文件中,标签里的width属性应该是switch_track_width而不是switch_track_height:
<size
  android:width="@dimen/switch_track_width"
  android:height="@dimen/switch_track_height" />

虽然这可以解决你的问题,但我建议在res/values-v21目录下再添加一个dimens.xml文件。
<dimen name="switch_track_width">30dp</dimen>  //change as per your view appreance
<dimen name="switch_track_height">25dp</dimen> //change as per your view appreance
<dimen name="switch_radius">50dp</dimen> //change as per your view appreance
<dimen name="switch_selector_padding">2dp</dimen>
<dimen name="switch_stroke_height">0dp</dimen>

你也可以在res/values/dimens.xml中更改宽度、高度和半径。
希望这能帮到你。

我已经按照你提到的进行了更改,但什么也没有发生...大小属性似乎由于某种原因被忽略了。 - AnZ

0

您可以使用这个小部件 "android.support.v7.widget.switchcompat"。它支持向后兼容。


似乎无法正常工作。已更改为“SwitchCompat”,现在看起来是这样的 - http://i.imgur.com/feh8eAf.png。现在所有API级别的大小都不会改变。 - AnZ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接