滚动时,Android背景列表视图变为黑色

时间:2012-11-21 10:59:44

标签: android android-layout

我创建了一个特定的Listview,它存在于以下元素之外,用于创建一个可滚动列表,每行包含左侧的Image和右侧的一些文本。我用于listview的xml是这样的:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:background="#C8C8C8"
>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:divider="#C8C8C8"
    android:background="#C8C8C8"/>

对于listview的行,我正在使用的xml代码如下所示:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/bg_row"
>
    <ImageView
        android:layout_width="wrap_content"
        android:paddingLeft="10px"
        android:paddingRight="15px"
        android:paddingTop="5px"
        android:paddingBottom="5px"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_image"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5px"
        android:paddingBottom="5px"
        android:textSize="16sp"
        android:textColor="#000000"
        android:layout_gravity="center"
        android:maxHeight="50px"/>
</LinearLayout>

只要屏幕静态显示(如没有移动),它就会正确显示,但是当我开始滚动列表时,行项目的背景(代码中可以显示的“图标”) )将被正确显示,但“根”布局的背景将变为完全黑色...当滚动停止时,背景将会,大多数时候,恢复它的颜色......当我测试时我也添加了一个TextView那个具有相同背景的根元素,这个会在滚动列表时保留它的颜色......任何想法为什么会这样,以及如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

在ListView标签上添加一个属性

android:cacheColorHint="#00000000" // setting as a transparent color

答案 1 :(得分:0)

您需要将ListView的缓存颜色设置为与ListView的颜色相同:

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:divider="#C8C8C8"
    android:background="#C8C8C8"
    android:cacheColorHint="#C8C8C8"  />

旁注:如果您希望分隔符不可见,您也可以按照以下步骤执行此操作:

android:divider="@null"