在 Lollipop 之前的 Android 版本中,有什么方法可以使开关的拇指高度大于轨道高度?
我本质上是在尝试在 Kitkat 中创建一个类似材料设计的开关小部件(以及旧版本 - 这里的问题是拇指默认“适合”在轨道内)。我已经为此工作了很长一段时间,但我似乎无法得出正确的设计。
我遵循了这篇 SO 帖子中的解决方案:如何更改 Switch Widget 的大小。
任何建议或解决方案将不胜感激。
在 Lollipop 之前的 Android 版本中,有什么方法可以使开关的拇指高度大于轨道高度?
我本质上是在尝试在 Kitkat 中创建一个类似材料设计的开关小部件(以及旧版本 - 这里的问题是拇指默认“适合”在轨道内)。我已经为此工作了很长一段时间,但我似乎无法得出正确的设计。
我遵循了这篇 SO 帖子中的解决方案:如何更改 Switch Widget 的大小。
任何建议或解决方案将不胜感激。
我在我的轨道可绘制形状中添加了一个透明笔触,这会导致轨道周围有一个填充:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/track_color"/>
<size android:height="@dimen/track_height" />
<size android:width="@dimen/track_width" />
<!-- results in the track looking smaller than the thumb -->
<stroke
android:width="6dp"
android:color="#00ffffff"/>
</shape>
是的,我解决了这个
<android.support.v7.widget.SwitchCompat
android:id="@+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF"
style="@style/Color1SwitchStyle"
android:text="" />
这是我的 Color1SwitchStyle
<style name="Color1SwitchStyle">
<item name="android:thumb">@drawable/customswitchselector</item>
<item name="android:track">@drawable/my_custom_track</item>
//when use Switch in xml file
<item name="track">@drawable/my_custom_track</item>
//when use android.support.v7.widget.SwitchCompat in xml
</style>
*听到的是我的 customswitchselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true"
android:drawable="@drawable/on" >
<shape
android:shape="rectangle"
android:dither="true"
android:useLevel="false"
android:visible="true">
<gradient
android:startColor="#66AAFF00"
android:endColor="#6600FF00"
android:angle="270"/>
<corners
android:radius="15dp"/>
<size
android:width="200dp"
android:height="80dp" />
<stroke
android:width="4dp"
android:color="#0000ffff"/>
</shape>
</item>
<item android:state_checked="false"
android:drawable="@drawable/off">
<shape
android:shape="rectangle"
android:dither="true"
android:useLevel="false"
android:visible="true">
<gradient
android:startColor="#66AAFF00"
android:endColor="#6600FF00"
android:angle="270"/>
<corners
android:radius="15dp"/>
<size
android:width="200dp"
android:height="80dp" />
<stroke
android:width="4dp"
android:color="#0000ffff"/>
</shape>
</item>
</selector>
听到很神奇 my_custom_track.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:state_enabled="true"
android:drawable="@drawable/switch_track_on" />
<item
android:state_checked="false"
android:state_enabled="false"
android:drawable="@drawable/switch_track_off" />
</selector>
switch_track_on.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:dither="true"
android:useLevel="false"
android:visible="true">
<gradient
android:startColor="@color/profile_pic_bg"
android:endColor="@color/profile_pic_bg"
android:angle="270"/>
<corners
android:radius="0dp"/>
<size
android:width="100dp"
android:height="10dp" />
<stroke
android:width="12dp"
android:color="#00ffffff"/>
</shape>
</item>
</layer-list>
switch_track_off.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:dither="true"
android:useLevel="false"
android:visible="true">
<gradient
android:startColor="@color/gray"
android:endColor="@color/gray"
android:angle="270"/>
<corners
android:radius="0dp"/>
<size
android:width="100dp"
android:height="10dp" />
<stroke
android:width="12dp"
android:color="#00ffffff"/>
</shape>
</item>
</layer-list>
感谢 stevenp 没有您的评论我无法解决这个问题
在“轨道”可绘制对象中添加顶部和底部插图对我有用。android:bottom="5dp"
android:top="5dp"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="5dp"
android:top="5dp">
<shape>
<corners android:radius="15dp" />
<solid android:color="@android:color/transparent" />
<stroke
android:width="2dp"
android:color="#EEEEEE" />
</shape>
</item>
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:track="@drawable/switch_track_drawable" />
我想出的解决方案只是为轨道使用 aline
而不是a 的形状rectangle
,并在其stroke
.
例如,如果开关定义如下:
<android.support.v7.widget.SwitchCompat
android:id="@+id/my_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:track="@drawable/switch_track"
app:theme="@android:style/Theme.DeviceDefault.Light" />
switch_track.xml 中的 drawable 使用您想要的任何宽度的线条形状:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line" >
<stroke android:color="#50bbb9bf" android:width="5dp"/>
</shape>
</item>
<item android:state_checked="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
<stroke android:color="#5000b6de" android:width="5dp"/>
</shape>
</item>
</selector>