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

7

是否有办法在Lollipop之前的Android版本中使开关的拇指高度大于轨道高度?

我基本上是想在Kitkat(和旧版本-这里的问题是默认情况下拇指'适合'轨道)中创建类似材料设计的开关小部件。我已经花了相当长的时间在这个问题上,但似乎无法得出正确的设计。

我遵循了这篇SO帖子上的解决方案:如何更改Switch Widget的大小

任何建议或解决方案将不胜感激。


为什么不使用APPCOMPAT来实现Material Design呢?如果这是你的目标! - Murtaza Khursheed Hussain
4个回答

32

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

<?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>

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

这是我的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的评论,否则我将无法解决这个问题

关闭开关

打开开关


1
在“轨迹”可绘制中添加顶部和底部插图对我有用。 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" />

0
我想到的解决方案是,为轨道使用一个形状为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>

嗨Drorsun,似乎不影响轨道的宽度/厚度。即使我将其增加到400dp,轨道的厚度仍然相同(我试图使轨道更厚)。有任何想法吗? - Sam
1
啊...找到问题了。我使用的是android:track="@drawable/switch_track"而不是app:track="@drawable/switch_track"。然而,android:shape="line"不起作用。我需要使用android:shape="rectangle"代替... - Sam

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