如何更改微调器弹出窗口的背景颜色? [英] How can I change the background color of a spinner popup?

查看:39
本文介绍了如何更改微调器弹出窗口的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置微调器弹出窗口的背景颜色,但我尝试过的所有操作都无法正常工作.

I'm trying to set the background color of a spinner popup but everything I've tried didn't work properly.

这是微调控件:

<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.

我用来填充弹出窗口的 xml 行是:

The xml line which I use to populate the popup is:

<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>

和可绘制背景 list_selector.xml:

and the drawable background 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> 

给上面的xml添加一个默认状态是可以的,但是spinner主控件用那个背景颜色显示项目,我不想要那个.

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.

我尝试的另一件事是在styles.xml 中将应用程序背景颜色设置为黑色

Another thing I've tried is to set the application background color to black in 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?

PS:我使用的是 API 级别 10 (2.3.3),并且属性 android:popupBackground 不存在.

PS: I'm using API level 10 (2.3.3) and the attribute android:popupBackground doesn't exist.

推荐答案

以上解决方案都不适合我.

None of the above solutions worked for me.

解决方案是在传递给微调器的适配器中编写方法 getDropDownView 中的不同实现,以使用自定义颜色显示不同的布局:

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:

@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;
}

这篇关于如何更改微调器弹出窗口的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆