2017-10-28 110 views
0

我想更改我的對話框微調框的popupBackground顏色。更改對話框微調框的popupbackground顏色

在我activity.xml

<Spinner 
        android:id="@+id/mCategorySpinner" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/textView7" 
        android:entries="@array/recipeCategory" 
        android:spinnerMode="dialog" 
        android:popupBackground="@color/colorPrimary" 
        android:textAlignment="center" /> 

在我activity.java

categorySpinner=(Spinner) findViewById(R.id.mCategorySpinner); 
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.recipeCategory, android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     categorySpinner.setPrompt("Choose category"); 
     categorySpinner.setAdapter(new NothingSelectedSpinnerAdapter(
       adapter, 
       R.layout.category_spinner_row_nothing_selected,    
       this)); 

什麼,如果我改變XML android:popupBackground發生,它仍然是默認的白色。
但是,如果我改變背景它的作品,但它不是對話框的背景。

+0

https://stackoverflow.com/questions/31425697/spinner-popup-background-color-issue –

+1

這裏閱讀:https://stackoverflow.com/questions/8922924/how-to-change-android -spinner-popupbackground –

回答

1

你可以改變背景顏色和下拉圖標喜歡做這樣

第1步:在繪製文件夾進行了微調的背景稱爲background.xml文件。

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="@android:color/transparent" /> 
    <corners android:radius="5dp" /> 
    <stroke 
     android:width="1dp" 
     android:color="@color/darkGray" /> 
</shape> 

第2步:現在申請此背景下你的微調在XML文件中

android:background="@drawable/background" 
+0

可繪製文件夾中的XML資源就像圖像一樣訪問,不需要在名稱後面添加'.xml'。這實際上導致它不被發現。 – Zoe

0

1.使用spinner_selector.xml

要顯示的顏色變化的

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@android:color/holo_red_light" 
      android:state_pressed="true"/> 
    <item android:color="@android:color/white" 
      android:state_pressed="false"/> 
</selector> 

2.添加樣式

將其添加到樣式中,您可以在其他位置使用它。

<style name="spinner_style"> 
    <item name="android:background">@drawable/spinner_selector</item> 
</style> 

3.增加它的XML代碼

使用它作爲微調的背景。

<Spinner 
    android:id="@+id/mCategorySpinner" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/textView7" 
    android:entries="@array/recipeCategory" 
    android:spinnerMode="dialog" 
    style="@style/spinner_style" 
    android:textAlignment="center" /> 
+0

沒有說明。現在有一些說明我刪除了downvote。請記住,並非每個在將來閱讀此文的人都確切地知道所有內容,解釋它非常重要。它仍然需要一個更好的解釋,但現在它已經足夠好了,它不會被低估 – Zoe

+0

'@ drawable/spinner_press'和'@ drawable/spinner'怎麼樣? – Chris

+0

我將它更改爲彩色。如果按下,顏色將會改變。@ Chris – KeLiuyue