简体   繁体   English

在棒棒糖前设备上使用SwitchCompat

[英]SwitchCompat on pre lollipop devices

I am trying to implement SwitchCompat from AppCompat but it looks different on different version devices. 我试图从AppCompat实现SwitchCompat,但它在不同的版本设备上看起来不同。 On Lollipop & Froyo it looks good but on Gingerbread to KitKat it doesn't look like a switch. 在棒棒糖和Froyo它看起来不错,但在姜饼到KitKat它看起来不像一个开关。

Code: 码:

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

Can I make these switches look the same across all versions or at least make them look like a switch? 我是否可以使这些开关在所有版本中看起来相同或者至少使它们看起来像一个开关?

Min sdk of my application was GingerBread and I had the same problem, finally I found the solution. 我应用程序的最小sdk是GingerBread,我有同样的问题,最后我找到了解决方案。 In order to make SwitchCompat consistent in all android versions I used two drawable at res/drawable folders, one for thumb and one for track . 为了使SwitchCompat在所有Android版本中保持一致,我在res/drawable文件夹中使用了两个drawable,一个用于thumb ,一个用于track and assign them to SwitchCompat in java code not in xml. 并将它们分配给不在xml中的java代码中的SwitchCompat Here is the code you should use. 这是您应该使用的代码。

SwitchCopmat widget: SwitchCopmat小部件:

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

drawable for thumb, switch_compat_thumb.xml drawable for thumb,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>

drawable for track , switch_compat_track.xml drawable for track ,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" />

and then, after finding it in java, assign thumb and track to SwitchCompat in java code: 然后,在java中找到它之后,在java代码中为SwitchCompat分配thumbtrack

  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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM