Android-GridView添加網格分割線


ListView 中設置分隔線的加如下參數即可:
android:divider="@color/gray"
android:dividerHeight="1dp"

GridView網格布局,默認情況下是沒有網格線的

查找網上資料,找到了一種為GridView添加網格線的小技巧

實際上,該網格線是通過設置GridView各子項的間隔,並分別設置GridView背景色與子項背景色實現的。

實現方法

  1. 設置GridView背景色,設置水平間方向間隔屬性值android:horizontalSpacing和豎直方向間隔屬性值android:verticalSpacing
  2. 設置GridView子項背景色

示例代碼:
1.main.xml

 <GridView
        android:id="@+id/gv_words"
        android:visibility="gone"
        android:background="@color/gray"
        android:columnWidth="60dp" 
        android:numColumns="5"
        android:listSelector="@null" 
        android:verticalSpacing="1.0px" 
        android:horizontalSpacing="1.0px"
        android:soundEffectsEnabled="true"
        android:smoothScrollbar="true"  
        android:stretchMode="columnWidth" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/llayout2"
        android:layout_below="@+id/llayout1"/>


2.GrivViewItem布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white" >
    <RelativeLayout
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="5dp">

          <TextView
            android:id="@+id/gv_bushou_TextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ItemImage"
            android:layout_margin="1dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_above="@+id/gv_bushou_TextView2"
            android:layout_toLeftOf="@+id/gv_bushou_TextView2"
            android:textSize="25dp"
            android:textColor="@color/blue"
            android:text="難" >
        </TextView>
   
        <TextView
            android:id="@+id/gv_bushou_TextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ItemImage"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:textColor="@color/gray"
            android:textSize="10dp"
            android:text="1筆" >
        </TextView>
    </RelativeLayout>
</LinearLayout>

3.運行截圖
GridView添加網格線


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM