5

AppCompat から SwitchCompat を実装しようとしていますが、異なるバージョンのデバイスでは異なって見えます。Lollipop & Froyo では良さそうに見えますが、Gingerbread to 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" />

これらのスイッチをすべてのバージョンで同じように見せたり、少なくともスイッチのように見せたりすることはできますか?

4

1 に答える 1

12

私のアプリケーションの最小SDKはGingerBreadで、同じ問題がありましたが、最終的に解決策を見つけました。すべての Android バージョンで一貫性を持たせるために、SwitchCompat2 つの drawable atres/drawableフォルダーを使用thumbしましたtrackSwitchCompatxmlではなくJavaコードでそれらを割り当てます。使用するコードは次のとおりです。

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>

の描画可能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" />

そして、Java でそれを見つけた後、Java コードでthumbandtrackを割り当てます。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));
于 2015-06-28T22:49:21.147 に答える