How to change spinner text color

后端 未结 8 1980
抹茶落季
抹茶落季 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: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;
    }
    

提交回复
热议问题