How to set border of GridView on Android

后端 未结 4 1844
小蘑菇
小蘑菇 2020-12-05 08:57

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

相关标签:
4条回答
  • 2020-12-05 09:20

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

    <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>
    
    0 讨论(0)
  • 2020-12-05 09:22

    Here are some examples of borders in a GridView.

    GridView Borders

    You can see where I defined the Red and Blue borders in my 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>
    

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

    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));
        }
    }
    
    0 讨论(0)
  • 2020-12-05 09:29

    I add these views around my gallery image in the image row 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
    0 讨论(0)
  • 2020-12-05 09:37

    Create grid_row_border.xml in the res/drawable folder.

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

    android:background="@drawable/grid_row_border"
    

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

    0 讨论(0)
提交回复
热议问题