Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

網格控制項 GridView 在 Android 中的應用

編輯:Android 開發實例

稍早已發表過一篇「使用 Gallery 實現縮圖瀏覽器」,今天要介紹的是另一款性質相近、但呈現方式截然不同的控制項。它以「網格」的形式排列內容,也就是說,當我們用它來展示一組圖片時,圖片會以類似九宮格的效果整齊鋪陳,而它的名字也正呼應了這個特性——網格控制項 GridView。

 

先來看看如何在版面配置中宣告這個控制項。

<GridView xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10px"
    android:verticalSpacing="10px"   //垂直間隔
    android:horizontalSpacing="10px"  //橫向間隔
    android:numColumns="auto_fit"  //自適應列數
    android:columnWidth="60px" //列寬
    android:stretchMode="columnWidth" //縮放與列寬大小同步
    android:gravity="center">
</GridView>

接著,只要為 GridView 指定一個自訂的轉接器就行了。這裡我們同樣用它來顯示圖片。

GridView gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this));

至於轉接器的具體定義,可以參考「使用 Gallery 實現縮圖瀏覽器」這篇文章——它的原理與功能幾乎完全一致,差別僅在於呈現的方式不同,因此這裡就不再贅述了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved