【问题标题】:How can I change the background color of a spinner popup?如何更改微调器弹出窗口的背景颜色?
【发布时间】:2020-08-07 03:58:46
【问题描述】:

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

这是微调控件:

<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添加一个默认状态是可以的,但是spinner主控件用那个背景颜色显示项目,我不想要那个。

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

【问题讨论】:

标签: android background popup android-spinner android-2.3-gingerbread


【解决方案1】:

在您的 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"
        >

【讨论】:

  • spinnerMode="dialog" 的时候呢? popupBackground 在该模式下似乎会被忽略。
【解决方案2】:

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

解决方案是在传递给微调器的适配器中编写方法 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;
}

【讨论】:

  • 由于某种原因,这在 Gingerbread 上不起作用 - 项目的背景似乎被覆盖了。在这种情况下,将文本颜色设为黑色会更容易。
  • +1 因为此解决方案适用于 API 15(我需要的那个),而 spinner.setPopupBackgroundDrawable() 仅适用于 API 16。
【解决方案3】:

效果很好!!

  • 用想要的颜色(和其他属性)声明布局与文本视图 想要改变)
  • textView 的 id 必须是 text1(因为这是在 R.android 中的默认布局 你的微调器)
  • 在您的微调器适配器中提供此布局

例如:

  • 在 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);
  • 然后将此适配器交给您的微调器

【讨论】:

    【解决方案4】:
      <Spinner
          android:id="@+id/myspinner"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/list_selector"      
          android:drawSelectorOnTop="true" />
    

    试试这个..

    【讨论】:

    • 您不能将 drawSelectorOnTop 与微调器一起使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多