2014-09-01 53 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); 

希望这有助于你。