2

私はAndroid開発の初心者です。

選択または押された場合、ListViewItem の FontColor と Background を変更しようとします。背景の変更は、次のセレクターを使用してまったく問題ありません。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_pressed="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:state_focused="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:state_selected="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:drawable="@android:color/transparent"></item>

</selector>

しかし、Background と FontColor を変更するにはどうすればよいですか?

4

2 に答える 2

2

TextView のフォントの色の変更は、listitem の backgroundcolor と同じ方法で行われます: StateListDrawable を作成し、をカスタム ドローアブルに設定しますTextViewandroid:textColor

例については、クリック時に ListViews のテキストの色を変更するを参照してください。これを、listitem の背景として既に持っているものと組み合わせます。

色同士をフェードさせたい場合は -tag などで設定android:exitFadeDuration="@android:integer/config_mediumAnimTime"して<selector>ください。

于 2012-04-05T15:45:30.413 に答える
0

これを試して

    @Override
public void onCreate(Bundle savedInstanceState) {
    TextView txtName;
    txtName.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView textView = (TextView) v;
            textView.setBackgroundColor(Color.RED);
            textView.setTextColor(Color.YELLOW);
        }
    });
}
于 2012-04-05T15:06:59.833 に答える