Can't change the text color with Android Action Bar drop-down navigation

前端 未结 6 2018
傲寒
傲寒 2020-12-28 11:04

I\'m using Drop-down Navigation with my Action bar, and I can\'t get a sensible color for the corresponding text when using a dark action bar. The action bar itself is a da

6条回答
  •  爱一瞬间的悲伤
    2020-12-28 11:31

    I've worked out that I can alter the colors for the drop-down Navigation in code, by creating my own subclass of ArrayAdapter as follows:

    public class myArrayAdaptor extends ArrayAdapter {
    
        public myArrayAdaptor(Context context, int textViewResourceId, T[] objects) {
            super(context, textViewResourceId, objects);
        }
    
        public static myArrayAdaptor createFromResource(Context context, int textArrayResId, int textViewResId) {
            CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
            return new myArrayAdaptor(context, textViewResId, strings);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return SetColourWhite(super.getView(position, convertView, parent));
        }
    
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            return SetColourWhite(super.getView(position, convertView, parent));
        }
    
        private View SetColourWhite(View v) {
            if (v instanceof TextView) ((TextView)v).setTextColor(Color.rgb(0xff, 0xff, 0xff)); // white
            return v;
        }
    }
    

    Then with the simple styles.xml

    
        
    
    

    I can substitute the class myArrayAdaptor

    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_dropdown_item)
    

    and the text color will always be white. But isn't there a simpler way of doing this using settings in the styles.xml file?

提交回复
热议问题