Android开关 - 使拇指高度大于轨道高度,如何?

use*_*702 5 android android-switch

在Lollipop之前的Android版本中,有没有办法让开关的拇指高度大于轨道高度?

我本质上是试图在Kitkat中创建一个类似材料设计的开关小部件(和旧版本一样 - 这里的问题是默认情况下拇指'适合'在轨道内).我已经在这方面工作了很长一段时间,但我似乎无法达到正确的设计.

我按照这个SO帖子上的解决方案:如何更改Switch Widget的大小.

任何建议或解决方案将是最受欢迎的.

小智 20

我在我的轨道可绘制形状上添加了一个透明笔划,导致在轨道周围填充:

<?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>
Run Code Online (Sandbox Code Playgroud)


sus*_*vi 5

是的,我解决了这个问题

 <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="" />
Run Code Online (Sandbox Code Playgroud)

这是我的 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>
Run Code Online (Sandbox Code Playgroud)

*听到的是我的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>
Run Code Online (Sandbox Code Playgroud)

我的 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>
Run Code Online (Sandbox Code Playgroud)

谢谢史蒂芬,没有你的评论我无法解决这个问题

关掉

打开