Android-listview-默认listitem(状态)背景色

Android-listview-默认listitem(状态)背景色,android,android-listview,android-styles,Android,Android Listview,Android Styles,当列表首次显示时,如何设置列表项的默认背景色 看起来很容易。。。但是等等 上下文:在一个模拟的弹出窗口中,我想显示一个项目列表。我想让他们有一个初始颜色的“软_红”(作为一个例子) 最好是通过XML布局文件进行设置 当然,我尝试了各种选择器示例。“按下”和“选择”工作正常。 但是出现之后。。。listitems的背景只是白色,而不是红色 我的代码: final PopupWindow popup = new PopupWindow( myActivity); popup.setContentVi

当列表首次显示时,如何设置列表项的默认背景色

看起来很容易。。。但是等等

上下文:在一个模拟的弹出窗口中,我想显示一个项目列表。我想让他们有一个初始颜色的“软_红”(作为一个例子)

最好是通过XML布局文件进行设置

当然,我尝试了各种选择器示例。“按下”和“选择”工作正常。 但是出现之后。。。listitems的背景只是白色,而不是红色

我的代码:

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); 
最终弹出窗口=新弹出窗口(myActivity);
popup.setContentView(布局);
... 
ListView m_ListView=(ListView)layout.findViewById(R.id.popup_菜单_列表);
ArrayAdapter=新的ArrayAdapter(myActivity,android.R.layout.simple\u list\u item\u 1,android.R.id.text1,menuItems);
m_listview.setAdapter(适配器);
布局文件:

<?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>

选择器代码(在列表\u selected\u flat\u color.xml中):


谢谢你的帮助

Pfff,找到了

看。这是一个很棒的教程

通过listview设置选择器不起作用。通过listview项设置选择器有效

总结。。。listitem条目:

<?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"/>

选择器。。。第一眼就能看到红色软菜单项:

<?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>

#ff0000
#ffffff
#FFBB33
#f7a1c3
列表视图:

<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); 
String[]myAddresses={“荷兰”、“西班牙”、“美国”};
ArrayAdapter=新的ArrayAdapter(mContext,R.layout.mable\u location\u list\u layout,myAddresses);
mPossibleLocationListView.setAdapter(适配器);

希望这对你有帮助

你说的弹出窗口是什么意思。单击列表项后,会显示一个带有颜色的对话框。弹出窗口首先使用列表视图创建。请参见上面的代码。因此,弹出窗口将显示一个白名单项目列表。我想让他们(在这个例子中)红软。
String[] myAddresses = { "Netherlands", "Spain", "US" };
ArrayAdapter<MyAddress> adapter = new ArrayAdapter<MyAddress>(mContext, R.layout.possible_location_list_layout, myAddresses);
mPossibleLocationListView.setAdapter(adapter);