How to change spinner text size and text color?

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

    For someone who needs only Style way for AppCompat.

    Result

    styles.xml

    <resources>
        ... 
        <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/material_grey_700</item>
            <item name="android:textSize">12sp</item>
        </style>
    </resources>
    

    your_spinner_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        ...
    
        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/content_spinner"
            style="@style/Widget.AppCompat.Spinner.Underlined"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:entries="@array/shipping_tracking_carrier_names"
            android:spinnerMode="dropdown"
            android:theme="@style/Spinner" />
    
        <EditText
            android:id="@+id/content_input"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:maxLines="1"
            android:paddingEnd="8dp"
            android:paddingStart="8dp"
            android:textColor="@color/material_grey_700"
            android:textSize="12sp" />
    
        ...
    </LinearLayout>    
    

    Plus
    And if you want to set android:entries programmatically with defined style.
    Try this.

    AppCompatSpinner spinner = findViewById(R.id.content_spinner);
    CharSequence[] entries = getResources().getTextArray(R.array.shipping_tracking_carrier_names);
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(spinner.getContext(), android.R.layout.simple_spinner_item, entries);
    adapter.setDropDownViewResource(android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    

    As in the code, using same Context with the Spinner is most important thing.

    spinner.getContext()
    
    0 讨论(0)
  • 2020-11-22 12:25

    Simplest: Works for me

    TextView spinnerText = (TextView) spinner.getChildAt(0);
    
    spinnerText.setTextColor(Color.RED);
    
    0 讨论(0)
  • 2020-11-22 12:26

    just add new style like this:

    <style name="mySpinnerItemStyle" parent="ThemeOverlay.AppCompat.Dark">
        <item name="android:textColor">#000</item>
        <item name="android:color">#000</item>
    </style>
    

    and use it:

    <Spinner
          android:id="@+id/spinnerCategories"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          style="@style/mySpinnerItemStyle"
          android:layout_margin="5dp" />
    
    0 讨论(0)
  • 2020-11-22 12:29

    Try this method. It is working for me.

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        TextView textView = (TextView) view;
        ((TextView) adapterView.getChildAt(0)).setTextColor(Color.RED);
        ((TextView) adapterView.getChildAt(0)).setTextSize(20);
        Toast.makeText(this, textView.getText()+" Selected", Toast.LENGTH_SHORT).show();
    }
    
    0 讨论(0)
  • 2020-11-22 12:30

    First we have to create the simple xml resource file for the textview like as below:

    <?xml version="1.0" encoding="utf-8"?>
    
     <TextView  
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="left"  
        android:textColor="#FF0000"         
        android:padding="5dip"
        />   
    

    and save it. after set on your adapterlist.

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

    Make a custom XML file for your spinner item.

    spinner_item.xml:

    Give your customized color and size to text in this file.

    <?xml version="1.0" encoding="utf-8"?>
    
    <TextView  
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="left"  
        android:textColor="#FF0000"         
        android:padding="5dip"
        />
    

    Now use this file to show your spinner items like:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
    

    You don't need to set the drop down resource. It will take spinner_item.xml only to show your items in spinner.

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