1

我正在一個項目上工作,我想改變旋鈕下拉選擇器的顏色。我使用下面的代碼。 android:dropDownSelector="#ffffff" 但它不工作,請提出任何方法。謝謝如何在android中更改下拉選擇器顏色的微調器?

這是我的代碼

<Spinner 
      android:id="@+id/spinner2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:spinnerMode="dropdown" 
      android:textColor="#ffffff" 
      android:textSize="19sp" 
      android:popupBackground="#80000000" 
      android:dropDownSelector="#ffffff" 
      android:background="@drawable/spinner" 
      android:layout_marginRight="13dp" 
      android:gravity="center_vertical" 
      android:layout_marginLeft="13dp" 
      android:layout_centerVertical="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 

view_spinner_item

<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:textColor="#8d95a7" 
    android:text="Gender" 
    android:textSize="17sp" 
    android:drawableRight="@drawable/iconarrow3" 
    /> 

微調代碼

Spinner spinner2 = (Spinner) findViewById(R.id.spinner2); 
    String[] items2 = new String[]{"Male","Female"}; 
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
      this, 
      R.layout.view_spinner_item, 
      items2 
    ); 
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_item); 
    spinner2.setAdapter(adapter2); 
+0

您可以使用自定義微調項目。 – Dexter

+0

我已經使用自定義微調 –

+0

我不認爲'dropDownSelector'是爲下拉的顏色... –

回答

-1

而不是使用android:dropDownSelector="#ffffff",你可以使用

android:backgroundTint="@android:color/holo_green_dark" 

如果api級別大於21 of course或使用自定義微調器下拉圖像。在style.xml

+0

這只是API 21+ –

1

添加如下代碼

<style name="AppSpinnerDropDownItem" parent="Base.Widget.AppCompat.DropDownItem.Spinner"> 
    <item name="android:background">@drawable/selector_spinner_item</item> 

</style> 

這自己的風格添加到您的應用程序的主題

 <item name="android:spinnerDropDownItemStyle">@style/AppSpinnerDropDownItem</item> 

selector_spinner_item.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checked="true" android:drawable="@drawable/layers_spinner_item_selected"></item> 
<item android:state_checked="false" android:drawable="@drawable/layers_spinner_item"></item> 
<item android:drawable="@drawable/layers_spinner_item"></item> 

可以也cu自定微調

0

我認爲這將有助於你

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //Simple Spinner Android Example 
     initSimpleSpinner(); 

     //Android Custom Spinner Example Programmatically 
     initCustomSpinner(); 

    } 

    private void initCustomSpinner() { 

     Spinner spinnerCustom = (Spinner) findViewById(R.id.spinnerCustom); 
     // Spinner Drop down elements 
     ArrayList<String> languages = new ArrayList<String>(); 
     languages.add("Andorid"); 
     languages.add("IOS"); 
     languages.add("PHP"); 
     languages.add("Java"); 
     languages.add(".Net"); 
     CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(SpinnerMainActivity.this, languages); 
     spinnerCustom.setAdapter(customSpinnerAdapter); 
     spinnerCustom.setOnItemSelectedListener(new OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

       String item = parent.getItemAtPosition(position).toString(); 

       Toast.makeText(parent.getContext(), "Android Custom Spinner Example Output..." + item, Toast.LENGTH_LONG).show(); 
      } 

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

      } 
     }); 
    } 

    private void initSimpleSpinner() { 
     // Spinner element 
     Spinner spinner = (Spinner) findViewById(R.id.spinner); 

     // Spinner Drop down elements 
     List<String> languages = new ArrayList<String>(); 
     languages.add("Andorid"); 
     languages.add("IOS"); 
     languages.add("PHP"); 
     languages.add("Java"); 
     languages.add(".Net"); 

     // Creating adapter for spinner 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, languages); 

     // Drop down layout style - list view with radio button 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     // attaching data adapter to spinner 
     spinner.setAdapter(dataAdapter); 
     spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

       String item = parent.getItemAtPosition(position).toString(); 

       Toast.makeText(parent.getContext(), "Android Simple Spinner Example Output..." + item, Toast.LENGTH_LONG).show(); 
      } 

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

      } 
     }); 
    } 


    public class CustomSpinnerAdapter extends BaseAdapter implements SpinnerAdapter { 

     private final Context activity; 
     private ArrayList<String> asr; 

     public CustomSpinnerAdapter(Context context, ArrayList<String> asr) { 
      this.asr = asr; 
      activity = context; 
     } 


     public int getCount() { 
      return asr.size(); 
     } 

     public Object getItem(int i) { 
      return asr.get(i); 
     } 

     public long getItemId(int i) { 
      return (long) i; 
     } 

     @Override 
     public View getDropDownView(int position, View convertView, ViewGroup parent) { 
      TextView txt = new TextView(SpinnerMainActivity.this); 
      txt.setPadding(16, 16, 16, 16); 
      txt.setTextSize(18); 
      txt.setGravity(Gravity.CENTER_VERTICAL); 
      txt.setText(asr.get(position)); 
      txt.setTextColor(Color.parseColor("#000000")); 
      return txt; 
     } 

     public View getView(int i, View view, ViewGroup viewgroup) { 
      TextView txt = new TextView(SpinnerMainActivity.this); 
      txt.setGravity(Gravity.CENTER); 
      txt.setPadding(16, 16, 16, 16); 
      txt.setTextSize(16); 
      txt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_launcher, 0); 
      txt.setText(asr.get(i)); 
      txt.setTextColor(Color.parseColor("#000000")); 
      return txt; 
     } 

    } 

,並添加您的繪製文件

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle" 
> 
    <solid android:color="@android:color/white"/> 


    <corners android:radius="4dp"></corners> 
    <stroke android:color="#cccccc" android:width="1dp"/> 

</shape> 

的Xml Filecode

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:padding="10dip" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dip" 
      android:text="Simple Spinner" 
      android:layout_marginBottom="5dp"/> 

     <Spinner 
      android:spinnerMode="dropdown" 
      android:id="@+id/spinner" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
     /> 


     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dip" 
      android:text="Custom Spinner" 
      android:layout_marginBottom="5dp"/> 

     <Spinner 
      android:background="@drawable/spinner" 
      android:spinnerMode="dropdown" 
      android:id="@+id/spinnerCustom" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
     /> 

    </LinearLayout> 
1

在一些舊版本的Android,這將改變所有的紡紗者的顏色,但如果這對你來說是好的mSpinner.getBackground().setColorFilter(ContextCompat.getColor(getContext(), R.color.blue), PorterDuff.Mode.SRC_ATOP);

相關問題