【问题标题】:How to add border to listview and different row colors at the same time如何同时为列表视图和不同的行颜色添加边框
【发布时间】:2013-03-18 22:55:54
【问题描述】:

我试图在我的列表视图外部设置一个边框,同时为每一行设置不同的颜色

这是我的适配器的 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>

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

【问题讨论】:

    标签: android listview border


    【解决方案1】:

    重写

    您显然已经阅读: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>
    

    【讨论】:

    • 这仍然会擦除我的边界:/
    • 谢谢,我设法实现了我想要的,我认为这是你刚才建议的,或者大致上,我会将我的代码添加到你的帖子中以帮助其他人:)跨度>
    • 你需要LinearLayout吗?您也许可以将backgroundgravitypadding 属性添加到ListView 并完全删除LinearLayout。但有时像background 这样的drawables...很挑剔,所以你可能需要它。
    • 我不记得为什么我需要它了,我想是因为当你选择一个项目时,它与边框重叠并且看起来很奇怪。
    • android:divider="@null" android:dividerHeight="0dp" 解答问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多