2013-07-30 20 views
8

Tôi đang sử dụng bộ điều hợp đơn giản để hiển thị mã của mình. Thật không may, tôi cần phải thay đổi màu textView trên cùng.Thay đổi màu chữ trong android.R.layout.simple_list_item_2

Đây là một đoạn mã của tôi:

// Keys used in Hashmap 
String[] from = { "txt1", "txt2" }; 
// Ids of views in listview_layout 
int[] ids = { android.R.id.text1, android.R.id.text2 }; 
SimpleAdapter adapter = new SimpleAdapter(this, aList, 
android.R.layout.simple_list_item_2, from, ids); 
setListAdapter(adapter); 

tôi đã cố gắng để làm cho simple_list_item_2 của riêng tôi, nhưng nó sẽ không cho phép tôi thay đổi màu sắc của một TextView trong xml đối với một số lý do. Có ý tưởng nào để làm việc này không?

suy nghĩ cuối cùng của tôi là:

findViewById(android.R.id.text1).setTextColor(#000) nhưng tôi không biết được nơi để đặt nó, và mã hex của tôi không hoạt động.

+0

Để chuyển màu hex, bạn sẽ phải sử dụng 'setTextColor (Color.parseColor (" # YOURCOLOR "))'. Tuy nhiên, điều này sẽ không hoạt động mà không có bộ điều hợp tùy chỉnh. –

Trả lời

1

Tạo xml Layout tùy chỉnh cho ListView mục của bạn và thiết lập màu chữ của TextView sử dụng thuộc tính textColor:

<TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TextView" 
      android:textColor="#ff0000" /> 
16

bạn phải ghi đè getView từ SimpleAdapter. Ví dụ:

SimpleAdapter adapter = new SimpleAdapter(this, aList, 
      android.R.layout.simple_list_item_2, from, ids) { 

     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = super.getView(position, convertView, parent); 
      TextView text1 = (TextView) view.findViewById(android.R.id.text1); 
      text1.setTextColor(Color.RED); 
      return view; 
     }; 
    }; 
+0

'ViewGroup không thể được giải quyết thành một loại' trong trường hợp của tôi – jaimin

+0

nó thực sự lạ @jaimin. Sẽ tốt hơn nếu bạn đăng câu hỏi mới giải thích sự cố của mình – Blackbelt

+1

Tôi không nghĩ đây là giải pháp tốt. Bạn không nên trộn logic ứng dụng với kiểu dáng. Sẽ tốt hơn nếu bạn định nghĩa bố cục tùy chỉnh như @FD_ được đề xuất. – Jeremy

-1

Bạn nên sử dụng setTextColor(Color.any color);

TextView txt = (TextView) view.findViewById(R.id.text1); 
txt.setTextColor(Color.yellow); 
0

Nếu bạn sử dụng một Spinner màu chữ thả xuống sẽ không thay đổi. Để thay đổi, chúng ta cũng phải thêm phương thức trên vào phương thức getDropDownView.

public View getDropDownView (int position, View convertView, ViewGroup parent) { 
                 View view = super.getDropDownView (position, convertView, parent); 
                 TextView text = (TextView) view.findViewById (android.R.id.text1); 
                 text.setTextColor (Color.BLACK); 
                 return view; 
             }