How to change the Spinner font color?

后端 未结 8 1389
盖世英雄少女心
盖世英雄少女心 2020-12-01 06:34

I\'m having an issue with the Droid X phones where users say that the font color turns out to be white in the spinner, making it invisible unless the users highlight the ite

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 07:20

    I'm going to use Spinner project sample from Android SDK for next code examples.


    Code:

    First, you need to create you custom adapter which will intercept the creation of views in drop down list:

    static class CustomArrayAdapter extends ArrayAdapter
    {
        public CustomArrayAdapter(Context ctx, T [] objects)
        {
            super(ctx, android.R.layout.simple_spinner_item, objects);
        }
    
        //other constructors
    
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent)
        {
            View view = super.getView(position, convertView, parent);
    
            //we know that simple_spinner_item has android.R.id.text1 TextView:         
    
            /* if(isDroidX) {*/
                TextView text = (TextView)view.findViewById(android.R.id.text1);
                text.setTextColor(Color.RED);//choose your color :)         
            /*}*/
    
            return view;
    
        }
    }
    

    Then you create adapter in your code like this:

     String [] spin_arry = getResources().getStringArray(R.array.Planets);        
     this.mAdapter = new CustomArrayAdapter(this, spin_arry);
    

    Explanation:

    Because CustomArrayAdapter knows that we use android's built-in layout resource, it also knows that text will be placed in TextView with id android.R.id.text1. That's why it can intercept the creation of views in drop down list and change text color to whatever color is needed.


    Screenshot:

    enter image description here

提交回复
热议问题