web-dev-qa-db-ja.com

Android=でスピナーのリストの背景色を変更する方法

新しいスピナーを動的に作成していますが、リストの背景色を変更するにはどうすればよいですか?

現在の背景色は濃い灰色です:

two muppets スピナーの背景属性を白に変更すると、次の望ましくない状況が発生します。

two muppets アクティビティで透明にしたいのですが、スピナーを開いているとき(それを押すとき)だけ、背景を白にしたいと思います。

これが私がスピナーを作成しているコードです:

私はアダプタを作成しています:

    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>

なにか提案を?

16
Noam Mizrachi

これを解決するには、スピナーを動的に作成するときにこのコードを追加します。

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>

このソリューションには、16以上のAPIレベルが必要です。

結果:

two muppets

12
Noam Mizrachi

この要件は、テーマの変更によっては実現できないと思います。 Spinnerコンストラクターは、layout 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" />

//ポップアップの背景を変更してみてください

23
Rahul Patil

あなたの問題を解決するには、これを試してください。

Android:popupBackground="#00826b"
2
Programer_saeed

私のspinner.xmlに

これをLinearLayoutで使用します:Android:background="#ffffff"

1
Dhaval Parmar