Android Spinner 的文本大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3329310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Text Size of a Spinner
提问by NickHalden
How can I decrease the font size of my spinner? I have reduced the spinner size to 35 pix because of which my text gets cut in half.
如何减小微调器的字体大小?我已将微调器大小减小到 35 像素,因此我的文本被切成两半。
How do i do that? Also I dont want anything to be selected beforehand.
我怎么做?我也不希望事先选择任何东西。
Default text should be "select some value"
.
默认文本应该是"select some value"
.
回答by Sephy
after some tests, there is an easier way than subclassing ArrayAdapter. Change the
经过一些测试,有一种比继承 ArrayAdapter 更简单的方法。改变
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 :
您只需要更改您使用 Spinner 的 TextView 的设计。我只是试过这个,作为 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>
回答by 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);
到 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);
回答by Autobots
define 2 xml: R.layout.spinner_item, R.layout.spinner_dropdown_item
定义 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
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
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);
回答by user1070356
Setting android:scaleX
and anrdoid:scaleY
seems like an easy way to change the spinner font size.
设置android:scaleX
和anrdoid:scaleY
似乎是更改微调字体大小的简单方法。