android列表视图 - 行颜色

时间:2012-10-21 17:36:35

标签: android

我正在尝试开发一个简单的Android应用程序。它有两个用于输入搜索条件的文本框。提交页面的提交按钮。在下一页(新意图)中,我显示搜索结果。

现在我想在列表视图中搜索结果。视图的xml代码如下所示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp">

 <ImageView
    android:id="@+id/image"
    android:layout_width="50px"
    android:layout_height="50px"
    android:layout_marginLeft="5px"
    android:layout_marginRight="20px"
    android:layout_marginTop="5px">
</ImageView>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" 
android:orientation="vertical">

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="15px" >
</TextView>
<TextView
    android:id="@+id/addr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="15px" >
</TextView>

在XXXArrayAdapter类的getView方法中,我将背景颜色设置如下。

  private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
 int colorPos = position % colors.length;
 rowView.setBackgroundColor(colors[colorPos]);

但是,我没有看到backgorund颜色填满整行。请看下面的截图。忽略红色框,我故意隐藏搜索结果。

1 个答案:

答案 0 :(得分:1)

您的布局的根LinearLayout宽度设置为换行内容。将其设置为match_parent,使其填满整个屏幕

<?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="wrap_content"
android:padding="5dp">
相关问题