2011-02-02 47 views
21

Tôi hiểu rằng máy quay đã đóng thực sự là View, tôi nghĩ vậy. Nhưng tôi đoán nó có một TextView ở đâu đó để hiển thị văn bản. Làm cách nào để truy cập vào số TextView để tôi có thể thay đổi màu văn bản?Màu văn bản của một spinner đã đóng

EDIT: Tôi cần thay đổi điều này một cách có lập trình khi đang chạy, không phải trong XML.

TextView v = (TextView) getView(mySpinner); 

v.setTextColor(..... 

doesnt làm việc này ...

Cảm ơn bạn!

array_typ=new String[5]; 
    array_typ[0]="Pressure"; 
    array_typ[1]="Level"; 

    array_typ[2]="Overage"; 
    array_typ[3]="Under"; 
    array_typ[4]="Taken"; 


    adaptertyp = new ArrayAdapter<Object>(this,R.layout.simple_spinner_item, array_typ); 
    typ.setAdapter(adaptertyp); 
+0

bạn không thể xác định màu trong file XML của nó? – gnclmorais

Trả lời

9

Tôi hiểu rằng spinner kín thực sự là một View, tôi nghĩ vậy.

Có. Cụ thể, đó là bất cứ điều gì bạn đã nói với SpinnerAdapter để tạo.

Nhưng tôi đoán nó có một TextView ở đâu đó để hiển thị văn bản.

Điều đó phụ thuộc vào những gì bạn đã nói với SpinnerAdapter để tạo.

Làm cách nào để có quyền truy cập vào TextView đó để tôi có thể thay đổi màu văn bản?

Lý tưởng nhất là bạn không - bạn đặt màu phù hợp ngay từ đầu, qua bất kỳ điều gì bạn đã nói với SpinnerAdapter để tạo. Nếu màu sắc thay đổi, hãy ghi đè getView() trong số SpinnerAdapter và thay đổi màu sắc tại thời điểm đó. Trong một nhúm, bạn có thể thử gọi getSelectedView() để hiển thị View hiện tại bằng cách đóng Spinner, nhưng bất kỳ thay đổi nào bạn thực hiện tại đây đều có thể bị loại bỏ trong lựa chọn tiếp theo của người dùng và màu thay thế có thể quay lại sau nếu trước đó View được tái chế.

+0

Tôi đã thêm cách tạo Spinner ở trên. Tôi thực sự mới với Android và Java vì vậy mặc dù câu trả lời của bạn có vẻ như có một câu trả lời tôi không biết phải nhập gì. Bạn có thể chỉ cho tôi một vài dòng mã sẽ cho phép tôi thay đổi màu chữ? cảm ơn!!! –

+0

@Mark Worsnop: "Tôi đã thêm cách tạo Spinner ở trên." - hãy xem tài nguyên 'R.layout.simple_spinner_item' của bạn và thay đổi màu trong đó. – CommonsWare

+0

Tôi cần thay đổi nó theo chương trình ... –

33

Để sửa đổi màu văn bản, hãy tạo tệp xml mới trong thư mục res/layout (ví dụ my_spinner_text.xml). Thêm một cái nhìn văn bản vào file xml mới và sửa đổi cách bạn muốn:

<TextView android:id="@+id/spinnerText" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:textColor="#CCCCCC" 
    android:textSize="20dp" 
    xmlns:android="http://schemas.android.com/apk/res/android"/> 

Tạo một ArrayAdapter mà sử dụng TextView mới và đặt nó vào spinner của bạn:

Spinner localSpinner = (Spinner)findViewById(R.id.mySpinner); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
       R.array.spinner_array, 
       R.layout.my_spinner_text); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    localSpinner.setAdapter(adapter); 
+0

Tôi cần có thể thay đổi màu sắc trong ứng dụng theo chương trình ... –

+1

đối với tôi, tôi phải thêm một không gian tên trong tệp xml ala

+4

Trang web được giới thiệu của bạn dường như ngoại tuyến. – rekire

1

để làm điều đó programatically bạn có để mở rộng các lớp adapter, một cái gì đó như:

ArrayAdapter<CharSequence> adapter = new ArrayAdater(this){ 
     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      View v = super.getView(position, convertView, parent);   
      // change the color here of your v 
      v. ... etc. etc   
     } 
    } 
+0

Tôi thấy một 'setBackgroundColor()', nhưng không có 'setTextColor()' –

23

Bạn có thể thay đổi màu sắc văn bản hoặc có thể truy cập vào TextView trong trường hợp setOnItemSelectedListener,

  spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
       ((TextView)parentView.getChildAt(0)).setTextColor(Color.rgb(249, 249, 249)); 

      } 
+0

công trình này! Những gì Budius đã đề xuất không phải vì tôi không thể đặt màu văn bản. –

+0

Điều này dường như không hiệu quả - thiết lập màu sắc mỗi lần. Bạn có thể đặt màu trên tất cả các mục trong spinner cùng một lúc bằng cách mở rộng 'ArrayAdapter' và ghi đè' getDropDownView() '. –

0

For Change Format của khép kín Spinner Tôi có làm theo cách này nó hoạt động

@Override 
public View getDropDownView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    if (view == null) { 
    LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    view = vi.inflate(R.layout.context_row_icon, null); 
    } 
    TextView mTitle = (TextView) view.findViewById(R.id.context_label); 
    ImageView flag = (ImageView) view.findViewById(R.id.context_icon);     

    mTitle.setText(values[position].getLabel(activity)); 

    if (!((LabelItem) getItem(position)).isEnabled()) { 
    mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled)); 
    } else { 
    mTitle.setTextColor(activity.getResources().getColor(R.color.context_item)); 
    } 
    return view; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    if (view == null) { 
    LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    view = vi.inflate(R.layout.context_row_icon, null); 
    } 
    TextView mTitle = (TextView) view.findViewById(R.id.context_label); 
    ImageView flag = (ImageView) view.findViewById(R.id.context_icon);     

    mTitle.setText(values[position].getLabel(activity)); 
    mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled)); 
    return view; 
} 
Các vấn đề liên quan