如何同时向listview和不同的行颜色添加边框

时间:2013-03-18 22:55:54

标签: android listview border

我正在尝试在listview的外部设置边框,并在同一时间为每隔一行设置不同的颜色

enter image description here

这是我的适配器的getView方法

        viewHolder.dateView.setText(entry.getDateString("yyyy-MM-dd HH:mm"));
        if(position % 2 == 0){
            viewHolder.linearLayout.setBackgroundResource(R.color.grey);

        }
        else{
            //viewHolder.linearLayout.setBackgroundResource(R.color.white);
        }

这是用于在 listview 上设置边框而不是单元格的xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <!-- use this for transparent -->
   <!-- <solid android:color="#00000000" /> -->
   <!-- use this for a background colour -->
   <solid android:color="@color/white" />
   <stroke android:width="2dip" android:color="@color/black"/>
</shape>

如果我将背景设置为单元格,则无法再看到边框

1 个答案:

答案 0 :(得分:5)

<强>重写

您显然已阅读:How do you put a border around a ListView?,因为您使用的是更好/更受欢迎的答案中的代码。但是也请阅读底部答案,我通过添加2dp填充(与边框宽度相同)获得了成功。

  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/border_listview"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="2dp" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp" />
</LinearLayout>