How to change spinner text size and text color?

后端 未结 24 1788
囚心锁ツ
囚心锁ツ 2020-11-22 11:35

In my Android application, I am using spinner, and I have loaded data from the SQLite database into the spinner, and it\'s working properly. Here is the code for that.

相关标签:
24条回答
  • 2020-11-22 12:16

    If you want a simple method, in order to add items to a dropdown, you usually add them to the strings.xml. Here is an example on how to add colour by using the strings.xml file:

    SELECT AGE RANGE

    <string-array name="age_array">
    
       <item> 0-6 </item>                               //No custom colour
    
      <item><font fgcolor='#FF4CD964'> 12+ </font></item> //With custom colour
    
    </string-array>
    
    0 讨论(0)
  • 2020-11-22 12:18

    we can change style of spinner textview set theme for spinner like this:

    styles.xml:

    <style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
        <item name="android:textSize">@dimen/_11ssp</item>
        <item name="android:textColor">@color/blue</item>
        <item name=...</item>
    </style>
    

    then

    <Spinner
                        android:theme="@style/mySpinnerItemStyle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
    

    if you want to change spinner textview attribute programtically:

    Programatically:

    val textView = (view.getChildAt(0) as TextView)
    textView.setTextColor(resources.getColor(R.color.dark_mode))
    
    0 讨论(0)
  • 2020-11-22 12:18
        String typeroutes[] = {"Select","Direct","Non Stop"};
        Spinner typeroute;
    
        typeroute = view.findViewById(R.id.typeroute);
    
        final ArrayAdapter<String> arrayAdapter5 = new ArrayAdapter<String>(
                    getActivity(), android.R.layout.simple_spinner_item, typeroutes) {
                @Override
                public boolean isEnabled(int position) {
                    if (position == 0) {
                        // Disable the first item from Spinner
                        // First item will be use for hint
                        return false;
                    } else {
                        return true;
                    }
                }
    
                public View getView(int position, View convertView, ViewGroup parent) {
                    View v = super.getView(position, convertView, parent);
                    ((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
                    ((TextView) v).setTextColor(Color.parseColor("#ffffff"));
                    return v;
                }         ---->in this line very important so add this
    
                @Override
                public View getDropDownView(int position, View convertView,
                                            ViewGroup parent) {
                    View view = super.getDropDownView(position, convertView, parent);
                    TextView tv = (TextView) view;
                    if (position == 0) {
                        // Set the hint text color gray
                        tv.setTextColor(Color.GRAY);
                    } else {
                        tv.setTextColor(Color.BLACK);
                    }
                    return view;
                }
            };
    
            arrayAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            typeroute.setAdapter(arrayAdapter5);
    

    that's all enjoy your coding...

    0 讨论(0)
  • 2020-11-22 12:19

    Simple and crisp...:

    private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    
           ((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
           ((TextView) parent.getChildAt(0)).setTextSize(5);
    
        }
    
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    };
    
    0 讨论(0)
  • 2020-11-22 12:19

    I have done this as following.I have use 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)
  • 2020-11-22 12:22

    To prevent lagging, you need to not only set the text properties in the onItemSelected listener, but also in the Activity's onCreate method (but it's a little tricky).

    Specifically, you need to put this in onCreate after setting the adapter:

    spinner.setSelection(0, true);
    View v = spinner.getSelectedView();
    ((TextView)v).setTextColor(backgroundColor);
    

    And then put this in onItemSelected:

    ((TextView) view).setTextColor(backgroundColor);
    

    Here is a full example:

    @Override  
    protected void onCreate(Bundle savedInstanceState)
    {  
        Spinner spinner = (Spinner) findViewById(R.id.spinner); 
    
        //Set the choices on the spinner by setting the adapter.  
        spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));
    
        //Set the text color of the Spinner's selected view (not a drop down list view) 
        spinner.setSelection(0, true);
        View v = spinner.getSelectedView();
        ((TextView)v).setTextColor(backgroundColor);
    
        //Set the listener for when each option is clicked.  
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {  
    
            @Override  
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {  
               //Change the selected item's text color  
               ((TextView) view).setTextColor(backgroundColor);
            }  
    
            @Override  
            public void onNothingSelected(AdapterView<?> parent)
            {  
            }  
        });  
    
    }  
    

    For more details, see my question.

    0 讨论(0)
提交回复
热议问题