如何在Android上设置GridView的边框 [英] How to set border of GridView on Android

查看:168
本文介绍了如何在Android上设置GridView的边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置GridView的边框。

如ListView的Divider和DividerHeight。

或者如何显示边框。

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

推荐答案

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

Here are some examples of borders in a GridView.

您可以在我的XML中看到我在哪里定义了红色和蓝色边框。

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

这是我的main.xml布局:

This is my main.xml Layout:

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

红色边框的厚度由GridView的 layout_margin 属性,并且蓝色边框由 horizo​​ntalSpacing verticalSpacing

The thickness of the Red border is controlled GridView's layout_margin attribute and the Blue borders are controlled by horizontalSpacing and verticalSpacing.

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

To make the black cell backgrounds I used this layout and saved it as 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));
    }
}

这篇关于如何在Android上设置GridView的边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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