Android 如何更改微调弹出窗口的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12606660/
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 can I change the background color of a spinner popup?
提问by Mike-Bell
I'm trying to set the background color of a spinner popup but everything I've tried didn't work properly.
我正在尝试设置微调弹出窗口的背景颜色,但我尝试过的一切都无法正常工作。
This is the spinner control:
这是微调控件:
<Spinner
android:id="@+id/myspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@null"
android:drawSelectorOnTop="true" />
When I click on it, it shows a popup with a white background, and I want to change that.
当我点击它时,它会显示一个白色背景的弹出窗口,我想改变它。
The xml line wich I use to populate the popup is:
我用来填充弹出窗口的 xml 行是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:paddingBottom="@dimen/padding_medium"
android:layout_marginBottom="@dimen/padding_medium"
android:orientation="vertical">
..........
</RelativeLayout>
and the drawable background list_selector.xml:
和可绘制背景 list_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@color/green" /> <!-- @drawable/tab_press -->
<!-- Selected -->
<item
android:state_selected="true"
android:drawable="@color/green" /> <!-- @drawable/tab_press -->
</selector>
Adding a default state to the above xml is ok, but the spinner main control shows the item with that background color, and I don't want that.
给上面的xml添加一个默认状态就可以了,但是spinner主控件显示了那个背景色的item,我不想要。
Another thing I've tried is to set the application background color to black in styles.xml
我尝试过的另一件事是在styles.xml 中将应用程序背景颜色设置为黑色
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:background">#000000</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:typeface">sans</item>
</style>
That overrides the popup background too, but it has undesirable collateral effects. Is there any way to accomplish this in a simple way?
这也覆盖了弹出背景,但它具有不良的附带影响。有没有办法以简单的方式实现这一点?
Thanks!
谢谢!
PS: I'm using API level 10 (2.3.3)and the attribute android:popupBackground
doesn't exist.
PS:我使用的是 API 级别 10 (2.3.3)并且该属性android:popupBackground
不存在。
采纳答案by Mike-Bell
None of the above solutions worked for me.
以上解决方案都不适合我。
The solution was writing in the adapter passed to the spinner a different implementation in the method getDropDownView to display a different layout with custom colors:
解决方案是在传递给微调器的适配器中编写 getDropDownView 方法中的不同实现,以显示具有自定义颜色的不同布局:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vista = convertView;
// layout for spinner widget
if (vista==null) {
LayoutInflater inflater = actividad.getLayoutInflater();
vista = inflater.inflate(R.layout.fila_colores_spinner, null);
}
return vista;
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
View vista = convertView;
//layout for spinner popup
if (vista==null) {
LayoutInflater inflater = actividad.getLayoutInflater();
vista = inflater.inflate(R.layout.fila_colores_spinner_popup, null);
}
return vista;
}
回答by Rudresh Ajgaonkar
Use android:popupBackground="@drawable/Yourxmlfile.xml" in Your spinner declaration of xml .
在 xml 的微调器声明中使用 android:popupBackground="@drawable/Yourxmlfile.xml" 。
For Eg.
对于例如。
<Spinner
android:id="@+id/spinner3"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_weight="50"
android:background="@drawable/gradient_spinner"
android:gravity="center"
android:paddingLeft="40dp"
android:popupBackground="@drawable/radialfront"
>
回答by dvrm
It works great!!
效果很好!!
- declare layout with text view with the wanted color (and other attributes you want to change)
- the id of the textView must be text1(cause this is the id that is declare in the default layout in R.android for your spinner)
- give this layout in your spinner adapter
- 使用具有所需颜色(以及您要更改的其他属性)的文本视图声明布局
- textView 的 id必须是 text1(因为这是在 R.android 的默认布局中为您的微调器声明的 id)
- 在您的微调适配器中提供此布局
for example:
例如:
in res\layout\custom_spinner_item.xml put this:
TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#B2B5B7" android:gravity="center_horizontal" android:textSize="20dp"
在 res\layout\custom_spinner_item.xml 中放置:
TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#B2B5B7" android:gravity="center_horizontal" android:textSize="20dp"
then give this layout to the adapter:
然后将此布局提供给适配器:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MyActivity.this,
R.layout.custom_spinner_item, strings);
- then give this adapter to your spinner
- 然后将此适配器提供给您的微调器
回答by bCliks
<Spinner
android:id="@+id/myspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:drawSelectorOnTop="true" />
try this..
尝试这个..