how do you set the spinner text color?

前端 未结 6 1005
庸人自扰
庸人自扰 2020-12-03 07:16

I have been unable to set the color on the spinner widget. How is this styled?

\"SpinnerText\"

相关标签:
6条回答
  • 2020-12-03 07:30

    If you want to change the text color : Spinner Widgets Text Color

    Else, making your own layout is the best way like JoxTraex said.

    0 讨论(0)
  • 2020-12-03 07:31

    Simple and crisp ....

    private OnItemSelectedListener your_spinner _name= new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos,
                long id) {
    
            ((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
    
    
        }
    
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    };
    
    0 讨论(0)
  • 2020-12-03 07:32

    The simplest form is:

    m_spnDia = (Spinner)findViewById(R.id.spiDia);
    TextView oTextView = (TextView)m_spnDia.getChildAt(0);
    oTextView.setTextColor(Color.RED);
    
    0 讨论(0)
  • 2020-12-03 07:38

    Try understanding that you are using the dropdown list as provided by the defaults that are available in the SDK.

    SIMPLY make your own layout with a custom adapter.

    0 讨论(0)
  • 2020-12-03 07:42

    Try using this adapter for your spinner:

    ArrayAdapter<String> adapter = 
        new ArrayAdapter<String>(Home.Home_Group, R.layout.my_spinner_style, yourstringarray)
    {
    
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
    
            ((TextView) v).setTextSize(16);
            ((TextView) v).setTextColor(
                getResources().getColorStateList(R.color.white)
            );
    
            return v;
        }
    
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View v = super.getDropDownView(position, convertView, parent);
            v.setBackgroundResource(R.drawable.spinner_bg);
    
            ((TextView) v).setTextColor(
                getResources().getColorStateList(R.color.spinner_text)
            );
    
            ((TextView) v).setTypeface(fontStyle);
            ((TextView) v).setGravity(Gravity.CENTER);
    
            return v;
        }
    };
    

    Add this xml to your layout,

    my_spinner_style.xml

    <?xml version="1.0" encoding="utf-8"?>
        <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
            android:id="@+android:id/text1"
            style="?android:attr/spinnerItemStyle"
            android:singleLine="true"
            android:textColor="#ffffff"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ellipsize="marquee" />
    

    And at last,

    spinner.setAdapter(adapter);
    
    0 讨论(0)
  • 2020-12-03 07:51

    A shorter alternative to Andro'd answer is to let the ArrayAdapter create the item views for you from a layout resource:

    final List<String> values = [SPINNER VALUES];
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(
        activity, R.layout.my_spinner_item, values);
    adapter.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    

    Then style your text to suit your needs in my_spinner_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="@style/my_spinner_item_style"
    />
    

    Note: my_spinner_dropdown_item is used when the list of choices appear

    For more information read the Spinners documentation.

    0 讨论(0)
提交回复
热议问题