How to change a spinner's list background color in Android

被刻印的时光 ゝ 提交于 2019-12-18 12:15:51

问题


I'm creating a new spinner dynamically, how can I change my list background color?

the current background color is some dark gray:

When I'm changing the spinner's background attribute to white, i'm getting this unwanted situation:

I want it to be transparent in the activity, and only when i'm opening the spinner (press on it), I want the background will be white.

Here is the code that I'm creating the spinner with:

I'm creating the adapter with:

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

my spinner.xml layout is:

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

any suggestion?


回答1:


I think this requirement can't possible through theme changes. Because Spinner constructor assigns value on popupBackground attr only if you write in layout xml otherwise it will use default theme value. like below

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

// just try changing popup background




回答2:


The solution for this is to add this code when creating the spinner dynamically:

spinner.setPopupBackgroundResource(R.drawable.spinner);

and to create spinner.xml under Drawable folder:

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

This solution require API level of 16 and above.

the outcome:




回答3:


To solve your problem,try this.

android:popupBackground="#00826b"



回答4:


in my spinner.xml

use this in LinearLayout : android:background="#ffffff"



来源:https://stackoverflow.com/questions/16198666/how-to-change-a-spinners-list-background-color-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!