2013-04-18 29 views
5

Tôi có một listview các mục văn bản đơn giản. Các TextView trong những mặt hàng giống như thế này (nó được bao bọc bên trong một RelativeLayout):Đặt màu văn bản Android TextView theo lập trình với setTextColor() hiển thị android: duplicateParentState không hợp lệ

<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" /> 

Tôi muốn sử dụng bộ chọn trạng thái màu sắc sau cho văn bản, mà tôi đã gọi là "dark_list_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/dark_list_text"/> 

</selector> 

Thông thường, tất nhiên, tôi chỉ có thể đặt thuộc tính android:textColor trong xml, nhưng trong trường hợp này, tôi cần đặt một mục cụ thể theo lập trình thành một màu khác, sử dụng công cụ chọn này ("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> 

Vì vậy, tôi đặt màu chữ trong Adaptor (language_nameselected được định nghĩa trước đó trong code):

 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); 
     } 

Và điều này hoạt động tốt, ngoại trừ một vấn đề: màu sắc văn bản không thay đổi khi mục được nhấp. Mặc dù tôi gọi setDuplicateParentStateEnabled(true) trên mặt hàng ngay sau setTextColor(), cài đặt đó hoàn toàn bị bỏ qua.

Tôi đã thử nghiệm điều này bằng cách bình luận ra mã Java đặt màu văn bản và chỉ cần đặt nó trong xml, và bộ chọn hoạt động tốt. Nhưng nếu tôi đặt textColor theo lập trình, nó hoàn toàn không có khả năng sao chép trạng thái gốc.

Có ai nhìn thấy điều này trước đây không? Có cách nào khác hay chỉ là lỗi Android mà tôi phải sống cùng?

Trả lời

21

OK, đã tìm thấy lỗi của tôi. Tôi nên sử dụng getResources().getColorStateList() thay vì getResources().getColor(). Để điều này ở đây cho bất kỳ ai khác mắc lỗi tương tự.

+0

chấp nhận điều này là câu trả lời. Nó hữu ích. –

+0

Cảm ơn bạn đã nhắc tôi. Làm xong. –

1

Từ colors.xml: textview.setTextColor(getResources().getColor(R.color.your_color));

Từ hệ điều hành Android: textview.setTextColor(Color.BLACK);

Enjoy :)

Các vấn đề liên quan