Android 如何更改微调器的文本样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3205387/
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
How do I change the text style of a spinner?
提问by Janusz
I'm creating a spinner in my layout xml files and setting an string array to this spinner. If I change the textstyle of the spinner the text is not affected by the changes.
我正在我的布局 xml 文件中创建一个微调器,并为这个微调器设置一个字符串数组。如果我更改微调器的文本样式,则文本不受更改的影响。
I read in the googlegroups that a spinner has no text and therefore the textstyle can not be changed and I have to change the style of the textview that is shown in the spinner. But how can I do that. Preferably in my xml file.
我在 googlegroups 中读到微调器没有文本,因此无法更改文本样式,我必须更改微调器中显示的文本视图的样式。但我怎么能做到这一点。最好在我的 xml 文件中。
回答by Oscar Salguero
As my predecessor specified, you can't do it on the main XML layout file where the Spinner component is.
正如我的前任所指定的,您不能在 Spinner 组件所在的主 XML 布局文件上执行此操作。
And the answer above is nice, but if we want to use Google's best practices, like you know... to use styles for everything... you could do it in 3 'easy' steps as follows:
上面的答案很好,但是如果我们想使用谷歌的最佳实践,就像你知道的那样......对所有东西都使用样式......你可以通过 3 个“简单”的步骤来完成,如下所示:
Step 1:You need an extra file under your layout folder with the look for the Spinner's items:
第 1 步:您的布局文件夹下需要一个额外的文件,用于查找 Spinner 的项目:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/textViewSpinnerItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/SpinnerTextViewItem"
xmlns:android="http://schemas.android.com/apk/res/android" />
Name this file: spinner_item_text.xml
将此文件命名为: spinner_item_text.xml
Step 2:Then, on your Activity Class when you are filling the Spinner with an array of items:
第 2 步:然后,在您的 Activity 类中,当您用一系列项目填充 Spinner 时:
adapter = new ArrayAdapter<CharSequence>(this, R.layout.spinner_item_text, items);
spinner.setAdapter(adapter);
Note that the R.layout.spinner_item_textresource is in your own R's file.
请注意,R.layout.spinner_item_text资源位于您自己的 R 文件中。
Step 3:Under your values folder, create or use (you might have one already) the file styles.xml. The style entry needed should look like this one:
第 3 步:在您的 values 文件夹下,创建或使用(您可能已经有了)文件 styles.xml。所需的样式条目应如下所示:
<style name="SpinnerTextViewItem" parent="@android:style/Widget.TextView" >
<item name="android:textSize" >8dp</item>
<item name="android:textStyle" >bold</item>
</style>
And that's it!
就是这样!
So far it has been really, really handy to put all about text sizes, styles, colors, etc... on a styles.xml file so it's easy to maintain.
到目前为止,将所有关于文本大小、样式、颜色等的信息放在一个styles.xml 文件中真的非常非常方便,因此易于维护。
回答by Joshua Pinter
Via XML Only
仅通过 XML
As a follow-up to @Cyril REAL's excellent answer, here is a thorough implementation of how to style your Spinners justthrough XML if you're populating your Spinner via android:entries
.
作为后续@Cyril REAL的出色答卷,这里是一个彻底的实现如何风格你纱厂只是通过XML如果您是通过填充你的微调android:entries
。
The above answers work if you're creating your Spinnervia code but if you're setting your Spinner entries via XML, i.e. using android:entries
, then you can adjust the text size and other attributes with the following two theme settings:
如果您通过代码创建Spinner,则上述答案有效,但如果您通过 XML 设置 Spinner 条目,即使用android:entries
,则可以使用以下两个主题设置调整文本大小和其他属性:
In your res/values/styles.xml
在你的 res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="android:Theme.Holo">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- For the resting Spinner style -->
<item name="android:spinnerItemStyle">
@style/spinnerItemStyle
</item>
<!-- For each individual Spinner list item once clicked on -->
<item name="android:spinnerDropDownItemStyle">
@style/spinnerDropDownItemStyle
</item>
</style>
<style name="spinnerItemStyle">
<item name="android:padding">10dp</item>
<item name="android:textSize">20sp</item>
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="spinnerDropDownItemStyle">
<item name="android:padding">20dp</item>
<item name="android:textSize">30sp</item>
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
回答by Robby Pond
When you create the Adapter that backs the Spinner you can set a layout for the spinner item.
当您创建支持 Spinner 的 Adapter 时,您可以为 Spinner 项目设置布局。
spinner.setAdapter(new ArrayAdapter(this, R.id.some_text_view));
You can style some_text_view the way you want.
您可以按照自己的方式设置 some_text_view 的样式。
<TextView android:id="@+id/some_text_view" android:textStyle="bold" />
回答by Cyril REAL
Actually you can customize spinner's text by xml.
实际上,您可以通过 xml 自定义微调器的文本。
In your own style, define a :
按照您自己的风格,定义一个:
<item name="android:spinnerItemStyle">@style/yourStyleForSpinnerItem</item>
and define this style also :
并定义这种风格:
<style name="yourStyleForSpinnerItem">
//stuff you want for the item style
</style>
When you instantiate the spinner's adapter in the java code, you can use the default android.R.layout.simple_spinner_item
在java代码中实例化spinner的适配器时,可以使用默认的android.R.layout.simple_spinner_item
回答by Vedavyas Bhat
If you don't want such workarounds, there's a simple way. Get the textview from the spinner and change its parameters:
如果您不想要这样的解决方法,有一个简单的方法。从 Spinner 获取 textview 并更改其参数:
TextView tv = (TextView) spin.getSelectedView();
tv.setTypeface(Typeface.DEFAULT_BOLD); //to make text bold
tv.setTypeface(Typeface.DEFAULT); //to make text normal
回答by Amir Hossein Ghasemi
if you want just change background of popup View in spinner, call this method:
如果您只想更改微调器中弹出视图的背景,请调用此方法:
spinner.setPopupBackgroundResource(R.color.pa_md_white);