Android:stateに応じてTextの色を変更する・・・ColorStateList

テキストの色をButton等のstateに合わせて変えることを、
xmlでやらずに自前でやるにはColorStateListを作成し、それをsetTextColorすれば良い。

以下、select状態で色が変わるTextViewを作成するサンプル

TextView text = new TextView(this);
text.setTextColor( new ColorStateList(
  new int[][] {
    new int[]{ android.R.attr.state_selected },
    new int[]{ -android.R.attr.state_selected },
  },
  new int[] {
    Color.argb(0xff,0xff,0xff,0xff),
    Color.argb(0xff,0x8f,0x8f,0x8f),
  }
) );

コメント

このブログの人気の投稿

nginxでlocalhostとしてアクセスをサーバーに転送する方法

Android・・・テキスト描画あれこれ, ascent(), descent()等

Android:stateに応じて切り替わるdrawable・・・StateListDrawable