SoFunction
Updated on 2025-03-11

Example of how to set row and column splitting lines with GridView in Android

Preface

Although Aandroid already has a RecyclerView, a very powerful View, which can be directly controlled into ListView and GridView, etc., and the frame lines are also relatively convenient, but in many cases, we have to still use GridView to implement layout. So how do we frame GridView at this time? The following two implementation methods are provided. You can choose.

1. Set vertical and horizontal spacing, and realize through the background colors of GRIDVIEW and ITEM

1. Set the background color of GridView

2. Set horizontal and vertical spacing:android:horizontalSpacingandandroid:verticalSpacing

3. Set the background color of the GridView item and its selected color

The XML code is as follows

<GridView 
 android: 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_below="@ id/textView1" 
 android:layout_marginTop="30dp" 
 android:background="#999999" 
 android:horizontalSpacing="0.5dp" 
 android:verticalSpacing="0.5dp" 
 android:padding="2dp" 
 android:numColumns="3" > 

item layout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:andro 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:gravity="center" 
 android:background="@android:color/whith"> 
 
 <TextView 
  android: 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textSize="20sp" 
  android:padding="10dp" 
  android:text="TextView" /> 
 
</LinearLayout> 

2. Set selector

This way is to set the item selectorandroid:backgroundImplemented by attributes

gv_selector code

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:andro> 
 <item 
  android:state_selected="true" > 
  <shape android:shape="rectangle"> 
    <solid 
    android:color="#CCCCCC" 
    /> 
    <stroke android:width="1.0px" android:color="#999999" /> 
  </shape> 
 </item> 
 <item 
  android:state_pressed="true" > 
  <shape android:shape="rectangle"> 
    <solid 
    android:color="#CCCCCC" 
    /> 
    <stroke android:width="1.0px" android:color="#999999" /> 
  </shape> 
 </item> 
 <item> 
  <shape android:shape="rectangle"> 
   <stroke android:width="1.0px" android:color="#999999" /> 
  </shape> 
 </item> 
</selector> 

At this point, the two methods have been written. If you are careful, you may find that the grid line in the middle of the second method is twice as thicker than the edge line. This is also a shortcoming point of the second method.

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.