如何更改android中微调器的下拉选择器颜色?

时间:2016-08-02 14:17:29

标签: android android-layout listview android-studio android-spinner

我正在处理一个项目,我想更改微调器下拉菜单的选择颜色。我使用以下代码。 android:dropDownSelector="#ffffff" 但它不起作用请建议任何方法。谢谢

这是我的代码

<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代码

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

4 个答案:

答案 0 :(得分:1)

在style.xml中添加以下代码

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

你也可以自定义微调器

答案 1 :(得分:1)

在某些旧版本的Android上,这会改变所有微调器的颜色,但是如果你可以的话mSpinner.getBackground().setColorFilter(ContextCompat.getColor(getContext(), R.color.blue), PorterDuff.Mode.SRC_ATOP);

答案 2 :(得分: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文件代码

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

答案 3 :(得分:-1)

您可以使用

而不是android:dropDownSelector="#ffffff"
android:backgroundTint="@android:color/holo_green_dark"

如果api级别大于21或使用自定义微调器下拉图像。