2014-09-01 93 views
1

如何在列表第一次顯示時設置列表項的默認背景顏色?Android - listview - 默認listitem(狀態)背景顏色

似乎很容易......但等待...

背景:在anmiated彈出窗口我想顯示的項目列表。我想讓它們具有「soft_red」的初始顏色(作爲示例)。

最好我想通過XML佈局文件來設置它。

當然,我嘗試了各種選擇器的例子。 「按下」和「選定」工作正常。 但彈出後...... listitems的背景只是白色,而不是red_soft。

我的代碼:

final PopupWindow popup = new PopupWindow(myActivity); 
popup.setContentView(layout); 
... 
ListView m_listview = (ListView) layout.findViewById(R.id.popup_menu_list); 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(myActivity, android.R.layout.simple_list_item_1, android.R.id.text1, menuItems); 
m_listview.setAdapter(adapter); 

佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/popupLinearLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:dividerHeight="2dp" 
    android:orientation="vertical" > 
    <ListView 
     android:id="@+id/popup_menu_list" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:listSelector="@drawable/list_selected_flat_colour" 
     android:layout_gravity="right" /> 
</LinearLayout> 

選擇器代碼(list_selected_flat_colour.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Normal state. WHY IS THIS NEVER USED --> 
    <item android:drawable="@color/red_soft" 
     android:state_pressed="false" 
     android:state_selected="false"/> 
    <!-- pressed state. OK --> 
    <item android:drawable="@color/pink_soft" 
     android:state_pressed="true" 
     android:state_selected="false"/> 
    <!-- Selected state. OK--> 
    <item android:drawable="@color/green_soft" 
     android:state_pressed="false" 
     android:state_selected="true"/> 
</selector> 

感謝您的幫助。

+0

什麼意思是彈出窗口。是否在單擊列表項後,您將顯示帶顏色的對話框。 – Psypher 2014-09-01 19:54:45

+0

首先創建彈出窗口並在其中顯示列表視圖。請參閱上面的代碼。所以,彈出窗口會出現一個白名單項目列表。我想讓他們(在這個例子中)red_soft。 – tjm1706 2014-09-02 05:28:26

回答

1

Pfff,發現它...

http://www.oneminuteinfo.com/2012/12/android-set-selection-color-in-listview.html。這是一個很好的教程。

通過listview設置選擇器不起作用。通過列表視圖設置選擇器item works!

總結...的列表項條目:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:minHeight="15dp" 
    android:textSize="15dp" 
    android:focusable="false" 
    android:background="@drawable/my_drawable"/> 

的選擇......這顯示了一見鍾情red_soft菜單項:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:drawable="@drawable/orange_color"/> 
    <item android:state_pressed="true" android:drawable="@drawable/white_color"/> 
    <item android:state_focused="true" android:drawable="@drawable/red_color"/> 
    <item android:drawable="@drawable/red_soft2"/> 
</selector> 

顏色:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <drawable name="red_color">#ff0000</drawable> 
    <drawable name="white_color">#ffffff</drawable> 
    <drawable name="orange_color">#ffffbb33</drawable> 
    <drawable name="red_soft2">#f7a1c3</drawable> 
</resources> 

列表視圖:

<ListView 
    android:id="@+id/possibleLocationView" 
    android:layout_width="fill_parent" 
    android:layout_height="80dp" 
    android:layout_marginBottom="10dp"/> 

適配器:

String[] myAddresses = { "Netherlands", "Spain", "US" }; 
ArrayAdapter<MyAddress> adapter = new ArrayAdapter<MyAddress>(mContext, R.layout.possible_location_list_layout, myAddresses); 
mPossibleLocationListView.setAdapter(adapter); 

希望這有助於你。