How to change spinner text size and text color?

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

    To change the color of spinner text :

     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);}
    
    0 讨论(0)
  • 2020-11-22 12:10

    Another variation of Ashraf's solution would be to make sure you're taking into account screen sizes. You'll need to get the spinner in onCreate and set the listener after you set the adapter:

    //set your adapter with default or custom spinner cell, then://
    serverSpinner.setOnItemSelectedListener(spinnerSelector);
    serverSpinner.setSelection(defaultServer);
    

    Then you can start changing the text size of the view that's showing before the spinner is clicked:

    private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
            boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
            if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
            else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
            else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
        }
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    };
    

    All you need to do is create layout specific folders like this:

    values-sw360dp

    values-sw600dp

    values-sw800dp

    an then add an xml file named "bool.xml" into each of those folders:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="isTablet">false</bool>
        <bool name="isLargeTablet">false</bool>
    </resources>
    
    0 讨论(0)
  • 2020-11-22 12:12

    The easiest way to re-use/change the android.R.layout resources is just go the definition. In Android Studio, do Ctrl + B on android.R.layout.simple_spinner_item.xml.

    It will take you to the resource file. Just copy the resource file and add a new layout in your Package.R.layout folder and change the textColor of textview as you like and then just call it in adapter like this:

    ArrayAdapter<String> adapter = new ArrayAdapter<String(Context,R.layout.spinner_item, spinnerlist);
    
    0 讨论(0)
  • 2020-11-22 12:12
    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@android:id/text1"
        style="?android:attr/spinnerItemStyle"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:ellipsize="marquee"
        android:textAlignment="inherit"/>
    

    just use this:

    ArrayAdapter<String> adapter_category = new ArrayAdapter<String>(this,
        R.layout.spinner_list_item, categories);
    adapter_category
        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
    0 讨论(0)
  • 2020-11-22 12:14

    If you work with android.support.v7.widget.AppCompatSpinner here is the simplest tested solution using styles:

     <android.support.v7.widget.AppCompatSpinner
                        android:id="@+id/spefcialFx"
                        style="@style/Widget.AppCompat.Spinner.Underlined"
                        android:layout_width="200dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="4dp"
                        android:theme="@style/Spinner"
                        android:entries="@array/special_fx_arrays"
                        android:textSize="@dimen/text_size_normal"></android.support.v7.widget.AppCompatSpinner>
    

    And the style:

    <style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
            <item name="android:paddingStart">0dp</item>
            <item name="android:paddingEnd">0dp</item>
            <item name="android:textColor">@color/white</item>
            <item name="android:backgroundTint">@color/red</item>
            <item name="android:textSize">14sp</item>
        </style>
    

    The only downside is the android:backgroundTint sets color for both the dropdown arrow and the dropdown background.

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

    Rather than making a custom layout to get a small size and if you want to use Android's internal small size LAYOUT for the spinner, you should use:

    "android.R.layout.simple_gallery_item" instead of "android.R.layout.simple_spinner_item".

    ArrayAdapter<CharSequence> madaptor = ArrayAdapter
                .createFromResource(rootView.getContext(),
                                    R.array.String_visitor,
                                    android.R.layout.simple_gallery_item);
    

    It can reduce the size of spinner's layout. It's just a simple trick.

    If you want to reduce the size of a drop down list use this:

    madaptor.setDropDownViewResource(android.R.layout.simple_gallery_item);
    
    0 讨论(0)
提交回复
热议问题