简体   繁体   中英

Spinner textSize doesn't change the fontsize in the app

I am using the xml in my layout folder to declare my spinners. For example:

<Spinner
    android:id="@+id/foo_spinner"
    android:layout_width="0sp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="30sp" />

I load the spinner entries within my code. My problem is that the text size isn't affected. I can put 100sp there but nothing will happen. Do I have to change it only after I load the spinner content? Do I need to do this after every time I update the content?

If you want to customize the text size Spinner item then you will have to use custom layout from Spinner item.

Suppose, create an custom TextView layout for Spinner item named custom_spinner_item.xml as follows...

<?xml version="1.0" encoding="utf-8"?>
<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:textSize="30sp" />

Now, use this layout XML to show your spinner items like:

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

Another thing, you should use dp or dip instead of sp for android:layout_width attribute and android:textSize isn't appropriate for Spinner element.

<Spinner
    android:id="@+id/foo_spinner"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM