Android-GridView添加網格分割線

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   ar   div   

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.運行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.