Android 如何更改列表视图的背景
我想改变listview的背景,到目前为止我已经尝试过了Android 如何更改列表视图的背景,android,android-listview,Android,Android Listview,我想改变listview的背景,到目前为止我已经尝试过了 <ListView android:id="@+id/myList" android:layout_height="wrap_content" android:layout_width="fill_parent" android:background="@color/white"/> 但这里的背景似乎不起作用。事实上,它显示了背景默认背景,没有任何实现方法。尝试更改每个listitem布局的颜色 设置适配器时,请查找列表
<ListView
android:id="@+id/myList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@color/white"/>
但这里的背景似乎不起作用。事实上,它显示了背景默认背景,没有任何实现方法。尝试更改每个listitem布局的颜色 设置适配器时,请查找列表项的xml:
public class YourListAdapter extends BaseAdapter {
/....Code for Adapter ...../
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = mInflator.inflate(R.layout.yourxml_listitem, null);
/...rest of code for adapter .../
然后编辑列表项的xml布局并为每个列表项添加背景色,因为我怀疑您正在设置不同的颜色/或者将颜色设置为透明 尝试将其设置为资源
setBackgroundResource(R.color.color\u red)您需要定义一个选择器First。如下所示, list_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@color/BackgroundColor" />
<item android:drawable="@color/white" />
</selector>
然后将此设置为您的背景
<ListView
android:id="@+id/myList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@drawable/list_bg"/>
如果您有此列表的自定义ListAdapter,则只需在该类的getView()方法中更改布局即可 如果没有,则需要在自定义主题内更改这两个参数
<item name="listChoiceBackgroundIndicator">@android:drawable/list_selector_background</item>
<item name="activatedBackgroundIndicator">@android:drawable/activated_background</item>
@android:drawable/list\u selector\u后台
@android:可绘制/激活的\u背景
我是在自定义列表适配器中完成的,因为我有一个不同的要求,即列表行的交替背景颜色等,并且它工作得很好。从未尝试过第二种选择,但我相信它应该有效。。。有关自定义主题的其他备选方案,请参见
编辑
我们不在string.xml中编写样式和主题声明,该文件仅用于字符串声明。在values文件夹中创建一个文件themes.xml,请参阅以下教程
请参见上面的任何示例。。创建主题后,您需要将该主题应用于android manifiest文件中的活动/应用程序。您还需要扩展任何现有主题
<ListView
android:id="@+id/myList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:cacheColorHint="#00000000"/>
如果使用自定义Listview,则在listitem latout中定义背景色
listitem.xml//Say
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/artists_list_backgroundcolor"
android:baselineAligned="false"
android:orientation="vertical" >
// Your Listitem widgets
</LinearLayout>
//您的Listitem小部件
像这样试试。你为什么不像下面这样试试呢
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="7dip"
android:background="@drawable/list_white_bg" >
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="170dip"
android:divider="#e0e0e0"
android:drawSelectorOnTop="false"
android:dividerHeight="1dip"
android:fadingEdge="none" >
</ListView>
</LinearLayout>
android:layout\u height=“fill\u parent”首先检查您的背景设置是否正确,否则setBackgroundResource会这样(确保您的列表视图位于所有其他列表视图之上)
setBackgroundResource(android.R.color.深色)
如果你喜欢你自己的颜色
listview.setBackgroundResource(R.color.Yellow)代码>
在ur/res/strings.xml文件中
#FF0
更多颜色
android 2.2上有一个bug。我发现最好的做法是在后面划船并将背景设置为空
<RelativerLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="@drawable/list_bg"/>
<ListView
android:id="@+id/myList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@null"/>
</RelativerLayout>
什么是@color/white?试着使用“@android:color/white”。@JaredBurrows对我不起作用。你能告诉我这个名字在哪里吗?一定是出了什么问题或更改了ListView的默认行为?@jared Burrows是的,我正在一个警报对话框中显示这个问题是的,我不知道如何解决这个问题。在我的例子中,listItems是在运行时动态添加的。如果您有任何想法,请建议我。请确保在运行时设置listItems的背景色。。使用setBackgroundResource(R.color.color_red)…我是android新手,我是否需要将其写入string.xml中,然后我必须在布局中引用它。是的,它只是更改分隔符的颜色,而不是背景颜色。@user2295109这是不同的问题,请尝试教程,它将帮助您。它不是那么简单。。。