如何改变一个微调的名单背景颜色的Andr​​oid [英] How to change a spinner's list background color in Android

查看:142
本文介绍了如何改变一个微调的名单背景颜色的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻找这个答案,但没有成功。

I searched for this answer but was unsuccessful.

我动态创建一个新的微调,怎样才能改变我的列表背景颜色?

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:

我希望它是透明的活动中,只有当我打开微调(上preSS),我希望背景将是白色的。

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.

下面是我创建与微调的code:

Here is the code that I'm creating the spinner 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);

我spinner.xml布局是:

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>

任何建议?

推荐答案

我解决了!

对此的解决方案是增加动态创建的微调时,此code:

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

spinner.setPopupBackgroundResource(R.drawable.spinner);

和创造下可绘制文件夹spinner.xml:

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>

这个解决方案需要16 API级及以上。

This solution require API level of 16 and above.

结果:

这篇关于如何改变一个微调的名单背景颜色的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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