在Android 5.0以下的设备上使用SwitchCompat控件

5

我正在尝试使用AppCompat中的SwitchCompat,但在不同版本的设备上看起来不同。

在Lollipop和Froyo上它看起来很好,但在Gingerbread到KitKat上它不像一个开关。

代码:

<android.support.v7.widget.SwitchCompat
        android:id="@+id/label_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="No"
        android:textOn="Yes"
        android:checked="false" />

我能否让这些开关在所有版本中看起来相同,或者至少让它们看起来像一个开关?


可能是重复的问题:Switchcompat不显示开关 - C--
接受的答案是使用Theme.AppCompat作为父级,但我只使用了Theme.AppCompat.NoActionBar(使用工具栏)。 - user3677365
把9patch和其他文件复制到项目中,让它正常工作怎么样?只有在您的项目没有任何问题的情况下才能这样做。 - C--
是的,我已经将它们(2个缺失的9 patch png文件)添加到drawable-hdpi中,但是没有起作用。这是因为我在3.4英寸屏幕的模拟器上测试吗? 在复制图像后,我需要指定什么吗? - user3677365
1个回答

12

我的应用程序的最小SDK版本是GingerBread,我遇到了同样的问题,最终我找到了解决方案。为了使SwitchCompat在所有Android版本中保持一致,我在res/drawable文件夹中使用了两个drawable,一个用于thumb,另一个用于track,并在Java代码中而不是XML中将它们分配给SwitchCompat。以下是你应该使用的代码。

SwitchCopmat小部件:

    <android.support.v7.widget.SwitchCompat
android:id="@+id/label_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

拇指可绘制对象,switch_compat_thumb.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:bottom="@dimen/switch_compat_thumb_margin"
    android:left="@dimen/switch_compat_thumb_margin"
    android:right="@dimen/switch_compat_thumb_margin"
    android:top="@dimen/switch_compat_thumb_margin">

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true">
            <shape android:shape="oval">
                <size
                    android:width="@dimen/switch_compat_thumb_size"
                    android:height="@dimen/switch_compat_thumb_size"/>
                <solid android:color="@android:color/red"/>
            </shape>
        </item>

        <item>
            <shape android:shape="oval">
                <size
                    android:width="@dimen/switch_compat_thumb_size"
                    android:height="@dimen/switch_compat_thumb_size"/>
                <stroke
                    android:width="@dimen/switch_compat_thumb_stroke_width"
                    android:color="@android:color/red"/>
                <solid android:color="@android:color/transparent" />
            </shape>
        </item>
    </selector>
</item>

轨道的可绘制资源,命名为 switch_compat_track.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="@dimen/switch_compat_track_radius"/>
<stroke
    android:width="@dimen/switch_compat_track_stroke_width"
    android:color="@android:color/red"/>
<solid android:color="@android:color/transparent" />

然后,在Java中找到它后,将thumbtrack分配给Java代码中的SwitchCompat:
  final SwitchCopmat switchCompat = (SwitchCopmat) findViewById(R.id.label_switch);

    //add thumb and track drawable in java since it doesn't work on xml for gingerbread
    switchCompat.setThumbDrawable(getResources().getDrawable(R.drawable.switch_compat_thumb));
    switchCompat.setTrackDrawable(getResources().getDrawable(R.drawable.switch_compat_track));

那对我来说几乎可以,但你能否包括从dimens中的定义?边距,宽度等。谢谢。 - shtolik
4
在 switch_compat_thumb.xml 中,项目(按钮、左、上、右)有 4dp 的大小,然后第一个椭圆形状的宽度和高度为 8dp,描边宽度为 4dp。第二个椭圆形状再次为 8dp(宽度和高度),描边宽度为 2dp。在 switch_compat_track.xml 中,矩形形状的圆角半径为 7dp,描边宽度为 2dp。 - Sherry

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