简体   繁体   English

如何更改列表视图的背景

[英]How to change the background of list view

I want to change the background of the listview so far i have tried this 到目前为止,我想更改列表视图的背景

<ListView
android:id="@+id/myList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@color/white"/>

but here the background doesn't seems to work.in fact its showing the back default back ground any idea how to achieve it . 但是这里的背景似乎没用。实际上它显示了默认的背景,任何想法如何实现它。

Try to change the color of each listitem layout. 尝试更改每个列表项布局的颜色。

When you are setting up the adapter find the xml for the list item: 设置适配器时,请找到列表项的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 .../

then edit the xml layout for your list item and add a background color to each list item, as I suspect you are setting up a different color / alternatively setup the color to transparent. 然后为您的列表项编辑xml布局,并为每个列表项添加背景色,因为我怀疑您要设置其他颜色/或者将颜色设置为透明。

尝试将其设置为资源setBackgroundResource(R.color.color_red)

You need to define a selector fisrt.Like this, list_bg.xml 您需要定义一个选择器fisrt,例如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>

Then set this as your background. 然后将其设置为背景。

 <ListView
android:id="@+id/myList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@drawable/list_bg"/>

If you have a custom ListAdapter for this list you can change the layout simply inside the getView() method of that class. 如果您对此列表具有自定义ListAdapter,则可以仅在该类的getView()方法内部更改布局。

If you have not than You need to change these two parameters inside your custom theme 如果您还没有,则需要在自定义主题中更改这两个参数

<item name="listChoiceBackgroundIndicator">@android:drawable/list_selector_background</item>

  <item name="activatedBackgroundIndicator">@android:drawable/activated_background</item>

I did it inside the Custom List Adapter since I had a different requirement ie alternate background colors of list rows etc and it worked nicely. 我在“自定义列表适配器”中完成了此操作,因为我有不同的要求,即列表行等的其他背景色,并且效果很好。 Never tried the second option but I belive it should work... see this link for other alternatives for custom themes. 从未尝试过第二个选项,但我相信它应该可以工作...有关自定义主题的其他替代方法,请参见此链接

EDIT We don't write style and theme declarations inside string.xml, that file is only for the string declarations. 编辑我们不在string.xml中编写样式和主题声明,该文件仅用于字符串声明。 Create a file themes.xml inside the values folder, see following tutorials 在values文件夹中创建一个文件themes.xml,请参阅以下教程

  1. A simple tutorial 一个简单的教程
  2. A Good tutorial 一个很好的教程
  3. The Best tutorial 最好的教程

See any of the above example.. Once theme is created you need to apply that theme to your Activity / Application inside android-manifiest file. 请参阅上面的任何示例。创建主题后,您需要将该主题应用于android-manifiedt文件中的Activity / Application。 You also need to extend any of the existing theme. 您还需要扩展任何现有主题。

<ListView
android:id="@+id/myList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:cacheColorHint="#00000000"/>

If you using Custom Listview then in listitem latout define the background color. 如果使用自定义Listview,则在listitem布局中定义背景颜色。

listitem.xml// Say listitem.xml //说

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

try like this.. 这样尝试..

Why dont you try like below lines. 你为什么不尝试下面的线。

<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_height="fill_parent" first check if ur background is set correct else setBackgroundResource like this (make sure ur listview is on top of all others) android:layout_height =“ fill_parent”首先检查您的背景设置是否正确,否则请像这样设置setBackgroundResource(确保您的listview位于所有其他视图的顶部)

listview.setBackgroundResource(android.R.color.darker_gray); listview.setBackgroundResource(android.R.color.darker_gray);

If u would like ur own colors 如果你想要你自己的颜色

listview.setBackgroundResource(R.color.Yellow);

and in ur /res/strings.xml file 并在ur /res/strings.xml文件中

<color name="Yellow">#FF0</color> for more colors http://en.wikipedia.org/wiki/Web_colors <color name="Yellow">#FF0</color>了解更多颜色http://en.wikipedia.org/wiki/Web_colors

There is a bug with that thing on android 2.2. 在android 2.2上有一个错误。 I found that the best practice will be punting an image behind and setting the background as null. 我发现最好的做法是在后面插入图像并将背景设置为空。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果未选中列表视图中的项目,如何在列表视图中更改背景颜色 - How to change background colour in List view if the item in list view is unselected ListView:如何从列表访问视图以更改背景颜色? - ListView: how to access a view from the list to change the background color? 如何在Android的适配器列表视图中更改背景颜色? - How to change background colour in adapter list view in android? 如何更改添加到列表视图中的最后一项的背景颜色? - how to change the background color of the last item added to the list view? 如何更改不再出现在屏幕上的可扩展列表视图子项的背景 - How to change the background of an expandable list view child that is not on screen anymore 如何在以选项为条件的列表视图中更改项目的背景色 - How to change a Background color of item in a list view conditioned by an option 如何使用列表视图列字段更改列表视图中行的背景颜色 - How to change Background Color of row in List View Using List View Column Field 如何改变视野中的孩子的背景 - How to change background of a child in view 动态更改列表视图项目背景 - Change list-view item background dynamically 使用背景更改的多选列表视图 - Multi select list view using background change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM