Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自定义道岔-轨道和选择器尺寸不低于21 API_Android_Xml_Android Switch - Fatal编程技术网

Android 自定义道岔-轨道和选择器尺寸不低于21 API

Android 自定义道岔-轨道和选择器尺寸不低于21 API,android,xml,android-switch,Android,Xml,Android Switch,自定义形状开关如下所示: 高于API 21 低于API 21 似乎块在中对21世纪之前的API不起作用 有什么办法解决这个问题吗? 代码 container.xml: <Switch android:id="@id/switch_follow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_a

自定义形状开关如下所示:

高于API 21

低于API 21

似乎
块在
中对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>
36dp
30dp
50dp
2dp
0dp

我已经复制了您的代码并尝试在我的机器中实现,您的drawable/switch\u selector.xml中
width属性的第一件事应该是switch\u track\u width,而不是switch\u track\u 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>
30dp//根据您的视图进行更改
25dp//根据您的意见进行更改
50dp//根据您的意见进行更改
2dp
0dp
还可以更改res/values/dimens.xml的宽度、高度和半径


希望对您有所帮助。

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

使用
标签一切正常。
可绘制的
已创建并正确应用。您的问题完全在
开关中

在旧版本中,在棒棒糖出现之前,拇指用于文本,可绘制的只是一个背景图像,可以缩放到所需的大小。您可以通过向
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>
30dp
15dp

请发布@dimen/switch\u track\u width and height size.@Warpzit,请检查我的编辑我认为(但不确定)这里的XML标记是错误的。除非它是由xml语法错误引起的(这似乎不是,特别是在以前的版本中,但这不是不可能的…),xml恰好是数据文件的格式用户,就像纯文本或“.ini”是在太多问题中使用的格式,实际上与此无关。如果我是你,我将通过代码更改它,你同意吗?我在定制开关控制方面做了很多工作,我使用了这个有很大潜力的库,只需检查一下:p我已经做了你提到的更改,但没有发生任何事情。。。由于某种原因,size属性似乎被忽略。似乎不起作用。改为
SwitchCompat
,现在看起来就是这样。所有API级别的大小现在都不会改变。请解释,谢谢!看来它解决了我的问题