私は単純なテキスト項目のリストビューを持っています。これらの項目でTextViewには、(それがRelativeLayout
内にラップです)次のようになります。Android TextViewのテキストの色をsetTextColor()でプログラムで設定するとアンドロイド:duplicateParentStateが無効
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@android:color/white"/>
<item android:color="@color/dark_list_text"/>
</selector>
:
<TextView
android:id="@+id/text_language_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:duplicateParentState="true"
android:textSize="16sp"
android:textStyle="bold" />
は私が「dark_list_text_states.xml」と呼ばれているテキスト、に対して次の色の状態セレクタを使用したいです
通常、xmlのandroid:textColor
プロパティに設定することはできますが、この場合は、このセレクタ(「blue_text_states.xml」)を使用して、特定の項目をプログラムで別の色に設定する必要があります。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@android:color/white"/>
<item android:color="@color/selected_blue"/>
</selector>
だから私は(language_name
とselected
は、以前のコードで定義されています)アダプターでテキストの色を設定します。
TextView text_language_name = (TextView)view.findViewById(R.id.text_language_name);
if (text_language_name != null) {
text_language_name.setText(language_name);
int text_color = selected
? getResources().getColor(R.color.blue_text_states)
: getResources().getColor(R.color.dark_list_text_states);
text_language_name.setTextColor(text_color);
text_language_name.setDuplicateParentStateEnabled(true);
}
そしてこれは一つの問題を除いて、正常に動作します:ときのテキストの色は変更されません。項目をクリックします。 setTextColor()
の直後に私がsetDuplicateParentStateEnabled(true)
と呼んでも、その設定は完全に無視されます。
私は、テキストの色を設定してxmlに設定するだけのJavaコードをコメントアウトすることでこれをテストしました。しかし、プログラムでtextColorを設定した場合、親状態を複製することはできません。
誰もこれまで見たことがありますか?回避策はありますか、これはちょうどAndroidのバグですか?
答えとしてこれを受け入れます。その有用な。 –
私に思い出させていただきありがとうございます。完了しました。 –