在ListView中使用RelativeLayout时删除了视图的背景颜色

时间:2015-09-01 00:46:33

标签: android android-layout android-listview colors android-relativelayout

在使用ListViews时遇到了一个有趣的问题。所以我的目标是为自定义ListView项目设计编写一个xml布局。

首先,这是有问题的xml布局(下面描述的问题):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="?android:attr/activatedBackgroundIndicator" >

    <TextView
        android:id="@+id/notebook_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"
        android:layout_toRightOf="@+id/notebook_color_tag"
        />

    <View
        android:id="@+id/notebook_color_tag"
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        />

</RelativeLayout>

布局非常基本,只有View,用于显示颜色TextView

我有一个ListFragment的子类,它在CursorAdapter方法中使用setListAdapter()的子类的实例。在该自定义CursorAdpater bindView()中,我设置了View的颜色和TextView的文字(该代码不是问题,你可以忽略它,它只是在这里参考):

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView notebookName = (TextView) view.findViewById(R.id.notebook_title);
    View colorTag = view.findViewById(R.id.notebook_color_tag);

    String name = cursor.getString(NOTEBOOK_NAME_POS);
    int color = cursor.getInt(NOTEBOOK_COLOR_POS);

    notebookName.setText(name);
    colorTag.setBackgroundColor(color);
}

现在问题是,如果View android:layout_height设置为match_parentwrap_content,则View根本不会出现在ListView项中。看起来它在顶部有一些余量。如果指定了具体的宽度值,例如:android:layout_height="40dp",一切正常。

以下是虚假布局的外观:

Erroneous ListView

以下是它的外观:

Correct ListView

如上所述,蓝色矩形似乎根本不在ListView项目容器内(但元素在那里,可通过findViewById()访问并且setBackgroundColor()通过View调用它的颜色会发生变化<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="?android:attr/activatedBackgroundIndicator" > <View android:id="@+id/notebook_color_tag" android:layout_marginTop="0dp" android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:id="@+id/notebook_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:textAppearanceLarge" /> </LinearLayout> 似乎根本无法显示。

对布局的以下更改解决了问题:

LinearLayout

我理解RelativeLayout在这里比RelativeLayout更有意义,但我很好奇{{1}}案例中的错误。

1 个答案:

答案 0 :(得分:1)

你有重复的笔记本_color_tag&#39; ID&#39;第您将在第一个布局XML中添加两次(作为TextView中的参数,也作为布局的子项)。

另外,我会颠倒顺序,如下:

<View
    android:id="@+id/notebook_color_tag"
    android:layout_width="20dp"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    />

<TextView
    android:id="@+id/notebook_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:textAppearanceLarge"
    android:layout_toRightOf="@id/notebook_color_tag"
    />

由于RelativeLayout将按照XML的顺序创建和放置,因此效率更高(无需重新测量以正确放置视图。