是否有办法在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="" />
<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>
<?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" />
line
而不是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 中使用一条线形状,设置所需的宽度:
<?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>
android:track="@drawable/switch_track"
而不是app:track="@drawable/switch_track"
。然而,android:shape="line"
不起作用。我需要使用android:shape="rectangle"
代替... - Sam