11

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

这是微调器控件:

<Spinner
  android:id="@+id/myspinner"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@null"
  android:drawSelectorOnTop="true" />

当我单击它时,它会显示一个带有白色背景的弹出窗口,我想更改它。

我用来填充弹出窗口的 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>

和可绘制的背景 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 添加默认状态是可以的,但是微调器主控件显示具有该背景颜色的项目,我不希望这样。

我尝试过的另一件事是在 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>

这也覆盖了弹出背景,但它具有不良的附带影响。有没有办法以简单的方式实现这一点?

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

4

4 回答 4

8

在 xml 的微调器声明中使用 android:popupBackground="@drawable/Yourxmlfile.xml" 。

例如。

    <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"
        >
于 2015-07-01T13:38:45.570 回答
6

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:

@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;
}
于 2013-03-29T19:09:33.777 回答
2

效果很好!!

  • 使用所需颜色(以及您要更改的其他属性)声明带有文本视图的布局
  • textView 的 id必须是 text1(因为这是在 R.android 的默认布局中为您的微调器声明的 id)
  • 在您的微调器适配器中提供此布局

例如:

  • 在 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"
    

然后将此布局提供给适配器:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
      MyActivity.this,
      R.layout.custom_spinner_item, strings);
  • 然后把这个适配器给你的微调器
于 2013-06-05T07:54:54.970 回答
0
  <Spinner
      android:id="@+id/myspinner"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/list_selector"      
      android:drawSelectorOnTop="true" />

尝试这个..

于 2013-02-27T04:44:17.413 回答