如何在Android上设置GridView的边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12047369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:34:49  来源:igfitidea点击:

How to set border of GridView on Android

androidgridview

提问by brian

How to set border of GridView.
Such as Divider and DividerHeight of ListView.
Or how to display the border.

如何设置 GridView 的边框。
比如ListView的Divider和DividerHeight。
或者如何显示边框。

回答by Sam

Here are some examples of borders in a GridView.

以下是 GridView 中边框的一些示例。

GridView Borders

GridView 边框

You can see where I defined the Red and Blue borders in my XML.

您可以看到我在 XML 中定义红色和蓝色边框的位置。

This is my main.xml Layout:

这是我的 main.xml 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/red" >

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"
        android:background="@color/blue"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp"
         />

</RelativeLayout>

The thickness of the Red border is controlled GridView's layout_marginattribute and the Blue borders are controlled by horizontalSpacingand verticalSpacing.

红色边框的粗细由 GridView 的layout_margin属性控制,蓝色边框由horizontalSpacing和控制verticalSpacing

To make the black cell backgrounds I used this layout and saved it as list_item.xml:

为了制作黑色单元格背景,我使用了这个布局并将其保存为 list_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="6dip"
    android:textAppearance="?android:attr/textAppearanceLarge" />

My Activity:

我的活动:

public class Example extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] array = new String[] {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
        List<String> list = new ArrayList<String>(Arrays.asList(array));
        GridView grid = (GridView) findViewById(R.id.gridview);
        grid.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));
    }
}

回答by Ganesh Katikar

use below xml file as background in grid item xml file.

使用下面的 xml 文件作为网格项 xml 文件中的背景。

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <corners
        android:bottomRightRadius="12dp"
        android:bottomLeftRadius="12dp"
        android:topLeftRadius="12dp"
        android:topRightRadius="12dp" />
    <stroke
        android:color="@android:color/white"
        android:width="1dp" />
</shape>

回答by Lucky

Create grid_row_border.xml in the res/drawable folder.

在 res/drawable 文件夹中创建 grid_row_border.xml。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <corners
        android:bottomRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
    <stroke
        android:color="@color/material_grey_600"
        android:width="1dp" />
</shape>

Now use this in you grid layout xml as below

现在在你的网格布局 xml 中使用它,如下所示

android:background="@drawable/grid_row_border"

Provide padding [android:padding="5dp"] and margin [android:layout_margin="5dp"] to look better.

提供内边距 [android:padding="5dp"] 和边距 [android:layout_margin="5dp"] 以看起来更好。

回答by Amjad Abu Saa

I add these views around my gallery image in the image row xml:

我在图像行 xml 中围绕我的画廊图像添加这些视图:

<View
    android:layout_width="@dimen/listGalleryItemWidthLarge"
    android:layout_height="2dip"
    android:layout_above="@+id/gallery_row_iv"
    android:layout_centerHorizontal="true"
    android:background="#FFFFFF" />

<View
    android:layout_width="@dimen/listGalleryItemWidthLarge"
    android:layout_height="2dip"
    android:layout_below="@+id/gallery_row_iv"
    android:layout_centerHorizontal="true"
    android:background="#FFFFFF" />

<View
    android:layout_width="2dip"
    android:layout_height="@dimen/listGalleryItemHeightLarge"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/gallery_row_iv"
    android:background="#FFFFFF" />

<View
    android:layout_width="2dip"
    android:layout_height="@dimen/listGalleryItemHeightLarge"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/gallery_row_iv"
    android:background="#FFFFFF" />
  • Note that I'm using a custom layout and adapter for my GridView
  • 请注意,我正在为我的 GridView 使用自定义布局和适配器