如何为gridview android提供单线边框 [英] How to give single line border to gridview android

查看:29
本文介绍了如何为gridview android提供单线边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created gridview with customer adapter. To give each cell border, I have placed them in two layouts. The first layout has black bg and second layout has white bg and contents. and I have given the parent layout 1dp padding, which given a border look

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

but the problem is that when two cells meet vertically, their border size becomes 2dp i.e. one cell's bottom border merges into other cell's top border.

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

But I like to create border as in given image

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

Here is code of my current cell's xml file

<?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

And don't forget to change you grid item layout height as match_parent

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

这篇关于如何为gridview android提供单线边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆