LoginSignup
4

More than 5 years have passed since last update.

Viewにselectorを設定のアレコレ

Posted at

忘備録に。
selectorの設定というと、viewのlayoutのxmlの中でbackgroundに指定する方法がよく出てくるが、textColorにも設定できる。
backgroundに設定するかtextColorに設定するかでselectorのcolorの指定の仕方が違うので注意する。

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_initial_footer_button"/>

これがbackgroundに設定するパターン

backgroundに設定したselectorは

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <color android:color="@color/white"/>
    </item>
    <item android:state_checked="false">
        <color android:color="@color/black"/>
    </item>
</selector>

このcolorはbackgroundの色が変わる

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@drawable/selector_initial_footer_button"/>

これがtextColorに設定するパターン

textColorに設定したselectorは


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/white"/>
    <item android:state_checked="false" android:color="@color/black"/>
</selector>

colorの属性は補完で出てこないのは何故なんだろう・・・。

これで文字色が変わる

[参考]
http://teik.in/?p=103

一つのbackgroundに設定したdrawableでtextColorも一緒に設定できたら便利だなぁ…。

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
4