微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何给gridview android提供单行边框

我用客户适配器创建了gridview.为了给每个单元格边框,我将它们放在两个布局中.第一个布局有黑色bg,第二个布局有白色bg和内容.我给了父布局1dp填充,给出了边框外观

但问题是当两个单元格垂直相交时,它们的边界大小变为2dp,即一个单元格的底部边界合并到其他单元格的顶部边界.

但我喜欢在给定的图像中创建边框

这是我当前单元格的xml文件代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layBorder"
    android:layout_width="77dp"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:orientation="vertical"
    android:padding="1dp" >

    <FrameLayout
        android:id="@+id/FrameLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF" >

        <ImageView
            android:id="@+id/ivElementName"
            android:layout_width="40dp"
            android:layout_height="60dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_margin="1dp"
            android:adjustViewBounds="true"
            android:background="#00000000"
            android:maxHeight="60dp"
            android:maxWidth="40dp"
            android:minHeight="60dp"
            android:minWidth="40dp" />

        <ImageView
            android:id="@+id/ivElementImg"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="bottom|center_vertical|center_horizontal"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:adjustViewBounds="true"
            android:background="#00000000"
            android:baselineAlignBottom="true"
            android:cropToPadding="true"
            android:maxHeight="30dp"
            android:maxWidth="30dp"
            android:minHeight="30dp"
            android:minWidth="30dp"
            android:visibility="invisible" />

    </FrameLayout>

</LinearLayout>

解决方法:

你应该做下一个

>设置gridview的背景颜色,它将是边框颜色
>根据需要设置网格项的背景颜色
>设置垂直和水平间距,它将是边框厚度

并且不要忘记将网格项布局高度更改为match_parent

GridView gv = findViewById(R.id.my_grid_view);
gv.setBackgroundColor(Color.WHITE);
gv.setVerticalSpacing(1);
gv.setHorizontalSpacing(1);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐