Change text color of selected item in spinner

后端 未结 14 1543
情深已故
情深已故 2020-11-29 06:17

How can I change the font color of the selected item in a spinner?

I am able to change the background color of the selected item, the color of the dropdown item etc,

14条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 07:04

    some of you that using MaterialBetterSpinner and Binding your Layouts, all the above won't help, try this, hope it helps you:

    public class MyAdapter extends ArrayAdapter {      
    
            public MyAdapter(Context context, int textViewResourceId, List objects) {
                super(context, textViewResourceId, objects);           
    
            }
    
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                return getCustomView(position, convertView, parent);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                return getCustomView(position, convertView, parent);
            }
    
            public View getCustomView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final YourXMLBinding rowBinding = DataBindingUtil.inflate(inflater, R.layout.yourXML, parent,false);
                rowBinding.tv1.setText(mMy.getValues().get(position));
                if(position == mMy.getCurrentIndex()) {
                    rowBinding.tv1.setTypeface(Typer.set(getContext()).getFont(Font.ROBOTO_BOLD));//change font
                    rowBinding.tv1.setTextColor(ContextCompat.getColor(getContext(), R.color.yourColor));//change color
                }
                return rowBinding.getRoot();
            }
        }
    

    yourXML is something like this:

    
    
    
        
            
    
    
    

    create a spinner with this adapter and yourXML :

    final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues());
    
    final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext());
    spinner.setAdapter(adapter);
    

提交回复
热议问题