How to change spinner text size and text color?

后端 未结 24 1738
囚心锁ツ
囚心锁ツ 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:34

    Here is a link that can help you to change the color of the Spinner:

    Click here

    <Spinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:textSize="20sp"
        android:entries="@array/planets"/>
    

    You need to create your own layout file with a custom definition for the spinner item spinner_item.xml:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#ff0000" />
    

    If you want to customize the dropdown list items, you will need to create a new layout file. spinner_dropdown_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="?android:attr/spinnerDropDownItemStyle"
        android:maxLines="1"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:ellipsize="marquee"
        android:textColor="#aa66cc"/>
    

    And finally another change in the declaration of the spinner:

    ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
    R.array.planets_array, R.layout.spinner_item);
    
    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
    spinner.setAdapter(adapter);
    

    That's it.

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

    For those who want to change DrowDownIcon color you can use like this

    spinner.getBackground().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
    
    0 讨论(0)
  • 2020-11-22 12:36

    If all the spinners may have the same text color for their TextView items, another approach is to use a custom style for spinner dropdown items:

    In res/values/styles.xml:

    <resources>
        <style name="AppBaseTheme" parent="android:Theme.Light">
        </style>
    
        <style name="AppTheme" parent="AppBaseTheme">
            <item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
        </style>
    
        <style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
            <item name="android:textColor">@color/my_spinner_text_color</item>
        </style>
    </resources>
    

    And define your custom color in res/values/colors.xml:

    <color name="my_spinner_text_color">#808080</color>
    
    0 讨论(0)
  • 2020-11-22 12:36

    Can change the text colour by overriding the getView method as follows:

     new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, list()){
                    @Override
                    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                        View view = super.getView(position, convertView, parent);
                        //change the color to which ever you want                    
                        ((CheckedTextView) view).setTextColor(Color.RED);
                        //change the size to which ever you want                    
                        ((CheckedTextView) view).setTextSize(5);
                        //for using sp values use setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                        return view;
                    }
        }
    
    0 讨论(0)
  • 2020-11-22 12:36

    you can have this type of adapter for spinner, totally customized:

     ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, genderList) {
    
            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("#676767"));
                ((TextView) v).setTypeface(vrFont);
                return v;
            }
    
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                View v = super.getDropDownView(position, convertView, parent);
                ((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
                ((TextView) v).setTypeface(vrFont);
                ((TextView) v).setTextColor(Color.parseColor("#676767"));
    
                if (position == 0) {
                    ((TextView) v).setTextColor(Color.parseColor("#979797"));
                }
    
                return v;
            }
    

    while R.layout.spinner_text is:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:gravity="center_vertical|left"
    android:ellipsize="marquee"
    android:maxLines="1"
    android:textColor="@color/whiteThree" />
    
    0 讨论(0)
  • 2020-11-22 12:37

    If you want the text color to change in the selected item only, then this can be a possible workaround. It worked for me and should work for you as well.

    spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    ((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE);
                }
            });
    
    0 讨论(0)
提交回复
热议问题