【问题标题】:How to change a spinner's list background color in Android如何在 Android 中更改微调器的列表背景颜色
【发布时间】:2013-04-24 17:38:21
【问题描述】:

我正在动态创建一个新的微调器,如何更改我的列表背景颜色?

当前的背景颜色是一些深灰色:

当我将微调器的背景属性更改为白色时,我遇到了这种不需要的情况:

我希望它在活动中是透明的,并且只有当我打开微调器(按下它)时,我希望背景是白色的。

这是我创建微调器的代码:

我正在创建适配器:

    mAdapter = new ArrayAdapter<String>(getApplicationContext(), 
                                     R.layout.spinner, R.id.Language, lang);
    LinearLayout layoutHolder = 
                         (LinearLayout)findViewById(R.id.RegisterFormLayout);
    Spinner spinner = new Spinner(getApplicationContext());
    LayoutParams layParams= new 
                Spinner.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,       
                                         ViewGroup.LayoutParams.WRAP_CONTENT);
    spinner.setLayoutParams(layParams);
    spinner.setAdapter(mAdapter); 
    spinner.setOnItemSelectedListener(new myOnItemSelectedListener());
    if (lang != null)
       spinner.setSelection(lang.intValue());
    spinnerList.add(spinner);
    layoutHolder.addView(spinner);

我的 spinner.xml 布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:id="@+id/SpinnerLayout">

<TextView
    android:id="@+id/Language"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#ffffff"
    android:background="#00ffffff"
    android:padding="5dp" />
</LinearLayout>

有什么建议吗?

【问题讨论】:

  • 您的每个列表视图项都有布局定义吗?
  • 不...这只是我附加到 ArrayAdapter(mAdapter) 的列表
  • @NoamMizrachi 在这里查看我的答案stackoverflow.com/questions/15299194/…
  • @Pragnani 看看我的编辑。我的 spinner.xml 文件有问题。我使 EditText 的背景透明,因为我希望它与我的活动的背景图像透明。现在,当我按下微调器时,我不知道深灰色从哪里到达我的背景微调器。我没有在任何地方声明这种颜色。有什么想法吗?
  • @NoamMizrachi 你为什么设置这个android:background="#00ffffff",黑底黑字?

标签: android android-spinner


【解决方案1】:

我认为这个要求不能通过主题更改来实现。因为 Spinner 构造函数仅当您在布局 xml 中写入时才会在 popupBackground attr 上分配值,否则它将使用默认主题值。如下所示

   <Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:popupBackground="@android:color/holo_green_dark" />

// 只是尝试改变弹出背景

【讨论】:

  • 它在它周围添加了一个讨厌的黑色边框
【解决方案2】:

解决方案是在动态创建微调器时添加此代码:

spinner.setPopupBackgroundResource(R.drawable.spinner);

并在 Drawable 文件夹下创建 spinner.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#ffffff" />
</shape>

此解决方案要求 API 级别为 16 及以上。

结果:

【讨论】:

  • 你对。 spinner.setPopupBackgroundResource(R.drawable.spinner);最低 API 级别为 16。要在所有级别上工作,请从 XML android:popupBackground="@android:color/red
【解决方案3】:

要解决你的问题,试试这个。

android:popupBackground="#00826b"

【讨论】:

    【解决方案4】:

    在我的 spinner.xml 中

    在 LinearLayout 中使用它:android:background="#ffffff"

    【讨论】:

    • 我得到的结果就像我的问题的第二张图片一样。我希望它在活动中是透明的,只有当我按下它时,背景才会是白色的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 2013-02-01
    • 1970-01-01
    相关资源
    最近更新 更多