Text Size of a Spinner

那年仲夏 提交于 2019-12-04 07:03:08

after some tests, there is an easier way than subclassing ArrayAdapter. Change the

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array,android.R.layout.simple_spinner_item);//this is from the tutorial, adapt with your line

to this line :

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.textview);

You just have to change the design of the TextView you use of the Spinner. I just tried with this, as the layout of the textview.xml :

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="@+id/TextView01" android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp"></TextView>
Sora

Changing

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array,android.R.layout.simple_spinner_item);

to ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.textview);

will change only the item which is visible by default on the spinner. If we need the same style to be applied on all the items in the spinner, we need to change

adapter.setDropDownViewResource(android.R.layout.simple_spinner_item)   to 


adapter.setDropDownViewResource(R.layout.textview);

define 2 xml: R.layout.spinner_item, R.layout.spinner_dropdown_item

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

spinner_item.xml

<?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"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:textSize="18sp" >
</TextView>

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:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:ellipsize="marquee"
    android:textSize="18sp" />

--------------------------------Update--------------------------------------

The rest code:

final ActionBar actionBar = getActivity().getActionBar();
RelativeLayout relative = new RelativeLayout(getActivity());
Spinner spinner = new Spinner(getActivity());
spinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));

relative.addView(spinner);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(relative);

// Set adapter and listener
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
user1070356

Setting android:scaleX and anrdoid:scaleY seems like an easy way to change the spinner font size.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!