LoginSignup
15
15

More than 5 years have passed since last update.

Theme.MaterialのSwitchをカスタマイズする際の注意

Last updated at Posted at 2014-12-03

もしHoloテーマのSwitchをカスタマイズしている場合、そのままMaterialテーマを適用してしまうと正常に表示できないことがあるので注意が必要です。

Thumb

Holoテーマでは下記のようなdrawableリソースをthumbに割り当ててつまみ画像を変更することができました。

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <corners android:radius="2dp"/>
            <solid android:color="@color/gray"/>
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <corners android:radius="2dp"/>
            <solid android:color="@color/orange"/>
        </shape>
    </item>
</selector>

switch_holo.png

ところがこの表記のままMaterialテーマを適用してしまうと、おそらくSwitchが表示されなくなってしまうと思います。
サイズ指定は忘れずに。

<shape android:shape="rectangle">
    <corners android:radius="2dp"/>
    <solid android:color="@color/orange"/>
    <size android:width="48dp"
          android:height="28dp"/>
</shape>

switch_material.png

Text

MaterialテーマではtextOn, textOffのテキストがデフォルトで表示されません。
showTextをtrueに設定しましょう。

15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15