How to change spinner text color

后端 未结 8 1968
抹茶落季
抹茶落季 2020-12-10 02:50

I saw many topics about how to change spinner\'s text color,but i couldnt understand how to use

spinner_item.xml



        
相关标签:
8条回答
  • 2020-12-10 03:38

    This is the simplest Method which I executed for spinner's text. I know its late but it will help some one.

    select_gender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      // give the color which ever you want to give to spinner item in this line of code               
                ((TextView)parent.getChildAt(position)).setTextColor(Color.parseColor("#646b99"));
                spinner_selected_gender=gender_list.get(position);
                Toast.makeText(getApplicationContext(),"You selected"+spinner_selected_gender,Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }
        });
    
    0 讨论(0)
  • 2020-12-10 03:42

    I have done this as following: I have used the getDropDownView() and getView() methods.

    Use getDropDownView() for opened Spinner.

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
         View view = convertView;
         if (view == null) {
            LayoutInflater vi = (LayoutInflater) activity
                             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.context_row_icon, null);
         }
         TextView mTitle = (TextView) view.findViewById(R.id.context_label);
         ImageView flag = (ImageView) view.findViewById(R.id.context_icon);               
    
         mTitle.setText(values[position].getLabel(activity));
    
         if (!((LabelItem) getItem(position)).isEnabled()) {
              mTitle.setTextColor(activity.getResources().getColor(
                   R.color.context_item_disabled));
         } else {
              mTitle.setTextColor(activity.getResources().getColor(
                  R.color.context_item));
         }
         return view;
    }
    

    And use getView() for closed Spinner.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         View view = convertView;
         if (view == null) {
             LayoutInflater vi = (LayoutInflater) activity
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             view = vi.inflate(R.layout.context_row_icon, null);
         }
         TextView mTitle = (TextView) view.findViewById(R.id.context_label);
         ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
    
         mTitle.setText(values[position].getLabel(activity));
    
         mTitle.setTextColor(activity.getResources().getColor(
               R.color.context_item_disabled));
    
         return view;
    }
    
    0 讨论(0)
提交回复
热议问题