2013-08-19 54 views
13

Tôi thấy nhiều chủ đề về làm thế nào để thay đổi màu sắc văn bản spinner, nhưng tôi couldnt hiểu làm thế nào để sử dụngLàm thế nào để thay đổi màu sắc văn bản spinner

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="top" 
    android:singleLine="true" 
    android:textColor="@color/iphone_text" /> 

gì thì tôi thực sự làm trong mã java :? Bất kỳ câu trả lời nào sẽ được aprecieted Vui lòng trả lời rõ ràng với chi tiết hơn khi bạn có thể

Trả lời

10

Tại đây Bạn phải đặt spinner_item.xml trong bộ điều hợp mảng của mình. Thêm đoạn mã này trong file java của bạn

Spinner yourSpinner; 
ArrayAdapter<String> yourAdapter; 
yourSpinner= (Spinner) findViewById(R.id.yourSpinnerID); 
yourSpinner.setAdapter(yourAdapter); 
yourAdapter= new ArrayAdapter<String>(this, R.layout.spinner_item, value); 
//here value is your items in spinner.. 
0
Spinner spnCategory= (Spinner)findViewById(R.id.my_spinner); 
.. 

ArrayAdapter<String> adptSpnCategory = new ArrayAdapter<String>this,R.layout.custom_spinner_item, alCategoryName); 
adptSpnCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spnCategory.setAdapter(adptSpnCategory); 
spnCategory.setOnItemSelectedListener(new OnItemSelectedListener() 
{ 
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
{ 
} 
public void onNothingSelected(AdapterView<?> arg0) 
{ 
} 
}); 
20

Một câu trả lời hoàn chỉnh cho tôi sẽ là một cái gì đó như:

public class ee extends Activity { 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ww); 
     addListenerOnSpinnerItemSelection(); 

    } 

    public void addListenerOnSpinnerItemSelection() { 
     ArrayList<String> array = new ArrayList<String>(); 
     array.add("item0"); 
     Spinner spinner1; 
     ArrayAdapter<String> mAdapter; 
     spinner1 = (Spinner) findViewById(R.id.spinner2); 
     mAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, array); 
     spinner1.setAdapter(mAdapter); 
    } 

} 

và trong res/layout thêm tập tin xml mới:

(trong spinner_item.xml)

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="top" 
    android:singleLine="true" 
    android:textColor="#00f0ff" /> 
+1

spinner1 in "spinner1 = new ArrayAdapter (this, R.layout.spinner_item, array);" lẽ ra phải là mAdapter. – Daniel

4
Spinner spinner = (Spinner) findViewById(R.id.spinner); 

spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View view, 
       int arg2, long arg3) { 

      ((TextView) arg0.getChildAt(0)).setTextColor(Color.RED); 
     } 
     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 

     } 
} 

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       R.layout.list_row, list); 
     spinner.setAdapter(adapter); 

list_row.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/text1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:paddingBottom="5dp" 
android:paddingLeft="10dp" 
android:paddingTop="5dp" 
android:textColor="#000000" 
android:textSize="20sp" /> 
0

Tôi đã làm điều này như sau: Tôi đã sử dụng các phương pháp getDropDownView()getView().

Sử dụng getDropDownView() để mở Spinner.

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

Và sử dụng getView() để đóng Spinner.

@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; 
} 
1

Đây là phương pháp đơn giản nhất mà tôi đã thực thi cho văn bản của spinner. Tôi biết nó muộn nhưng nó sẽ giúp một số.

select_gender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
    // give the color which ever you want to give to spinner item in this line of code    
      ((TextView)parent.getChildAt(position)).setTextColor(Color.parseColor("#646b99")); 
      spinner_selected_gender=gender_list.get(position); 
      Toast.makeText(getApplicationContext(),"You selected"+spinner_selected_gender,Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 
0

Từ cấp API 16 trở lên, bạn có thể sử dụng mã sau để thay đổi biểu tượng thả xuống trong spinner. chỉ cần goto onItemSelected trong setonItemSelectedListener và thay đổi drawable của textview được chọn như thế này.

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     // give the color which ever you want to give to spinner item in this line of code 

    //API Level 16 and above only. 
      ((TextView)parent.getChildAt(position)).setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,ContextCompat.getDrawable(Activity.this,R.drawable.icon),null); 

//Basically itis changing the drawable of textview, we have change the textview left drawable. 
      } 
      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
      } 
     }); 

hy vọng nó sẽ giúp ai đó.

0

Xây dựng trên câu trả lời của noobProgrammer, vì singleLine đã không được chấp nhận, một phiên bản cập nhật nhanh của spinner_item.xml cho bất kỳ ai đang tìm kiếm câu trả lời cho điều này.

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:maxLines="1"> 
</TextView> 
Các vấn đề liên quan