GridView跟ListView都是比較常用的多控制項布局,而GridView更是實現九宮圖的首選!本文就是介紹如何使用GridView實現九宮
圖。GridView的用法很多,網上介紹最多的方法就是自己實現一個ImageAdapter繼承BaseAdapter,再供GridView使用,
類似這種的方法本文不再重複,本文介紹的GridView用法跟前文ListView的極其類似。。。。也算是我偷懶一下。
先來貼出本文代碼啟動並執行結果:
本文需要添加/修改3個檔案:main.xml、night_item.xml、JAVA原始碼。
main.xml原始碼如下,本身是個GirdView,用於裝載Item:
view plaincopy to clipboardprint?
- <?xml version="1.0" encoding="utf-8"?>
- <GridView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/gridview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:numColumns="auto_fit"
- android:verticalSpacing="10dp"
- android:horizontalSpacing="10dp"
- android:columnWidth="90dp"
- android:stretchMode="columnWidth"
- android:gravity="center"
- />
<?xml version="1.0" encoding="utf-8"?><br /><GridView xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:id="@+id/gridview"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:numColumns="auto_fit"<br /> android:verticalSpacing="10dp"<br /> android:horizontalSpacing="10dp"<br /> android:columnWidth="90dp"<br /> android:stretchMode="columnWidth"<br /> android:gravity="center"<br />/>
介紹一下裡面的某些屬性:
android:numColumns="auto_fit" ,GridView的列數設定為自動
android:columnWidth="90dp",每列的寬度,也就是Item的寬度
android:stretchMode="columnWidth",縮放與列寬大小同步
android:verticalSpacing="10dp",兩行之間的邊距,如:行一(NO.0~NO.2)與行二(NO.3~NO.5)間距為10dp
android:horizontalSpacing="10dp",兩列之間的邊距。
接下來介紹
night_item.xml,這個XML跟前面ListView的ImageItem.xml很類似:
view plaincopy to clipboardprint?
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip" android:layout_width="fill_parent">
- <ImageView
- android:layout_height="wrap_content"
- android:id="@+id/ItemImage"
- android:layout_width="wrap_content"
- android:layout_centerHorizontal="true">
- </ImageView>
- <TextView
- android:layout_width="wrap_content"
- android:layout_below="@+id/ItemImage"
- android:layout_height="wrap_content"
- android:text="TextView01"
- android:layout_centerHorizontal="true"
- android:id="@+id/ItemText">
- </TextView>
- </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><br /><RelativeLayout<br /> xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_height="wrap_content"<br /> android:paddingBottom="4dip" android:layout_width="fill_parent"><br /> <ImageView<br /> android:layout_height="wrap_content"<br /> android:id="@+id/ItemImage"<br /> android:layout_width="wrap_content"<br /> android:layout_centerHorizontal="true"><br /> </ImageView><br /> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_below="@+id/ItemImage"<br /> android:layout_height="wrap_content"<br /> android:text="TextView01"<br /> android:layout_centerHorizontal="true"<br /> android:id="@+id/ItemText"><br /> </TextView><br /></RelativeLayout><br />
最後就是JAVA的原始碼了,也跟前面的ListView的JAVA原始碼很類似,不過多了“選中”的事件處理:
view plaincopy to clipboardprint?
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- GridView gridview = (GridView) findViewById(R.id.gridview);
-
- //產生動態數組,並且轉入資料
- ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
- for(int i=0;i<10;i++)
- {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("ItemImage", R.drawable.icon);//添加映像資源的ID
- map.put("ItemText", "NO."+String.valueOf(i));//按序號做ItemText
- lstImageItem.add(map);
- }
- //產生適配器的ImageItem <====> 動態數組的元素,兩者一一對應
- SimpleAdapter saImageItems = new SimpleAdapter(this, //沒什麼解釋
- lstImageItem,//資料來源
- R.layout.night_item,//night_item的XML實現
-
- //動態數組與ImageItem對應的子項
- new String[] {"ItemImage","ItemText"},
-
- //ImageItem的XML檔案裡面的一個ImageView,兩個TextView ID
- new int[] {R.id.ItemImage,R.id.ItemText});
- //添加並且顯示
- gridview.setAdapter(saImageItems);
- //添加訊息處理
- gridview.setOnItemClickListener(new ItemClickListener());
- }
-
- //當AdapterView被單擊(觸控螢幕或者鍵盤),則返回的Item單擊事件
- class ItemClickListener implements OnItemClickListener
- {
- public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened
- View arg1,//The view within the AdapterView that was clicked
- int arg2,//The position of the view in the adapter
- long arg3//The row id of the item that was clicked
- ) {
- //在本例中arg2=arg3
- HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);
- //顯示所選Item的ItemText
- setTitle((String)item.get("ItemText"));
- }
-
- }
public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> GridView gridview = (GridView) findViewById(R.id.gridview);</p><p> //產生動態數組,並且轉入資料<br /> ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();<br /> for(int i=0;i<10;i++)<br /> {<br /> HashMap<String, Object> map = new HashMap<String, Object>();<br /> map.put("ItemImage", R.drawable.icon);//添加映像資源的ID<br />map.put("ItemText", "NO."+String.valueOf(i));//按序號做ItemText<br /> lstImageItem.add(map);<br /> }<br /> //產生適配器的ImageItem <====> 動態數組的元素,兩者一一對應<br /> SimpleAdapter saImageItems = new SimpleAdapter(this, //沒什麼解釋<br /> lstImageItem,//資料來源<br /> R.layout.night_item,//night_item的XML實現</p><p> //動態數組與ImageItem對應的子項<br /> new String[] {"ItemImage","ItemText"}, </p><p> //ImageItem的XML檔案裡面的一個ImageView,兩個TextView ID<br /> new int[] {R.id.ItemImage,R.id.ItemText});<br /> //添加並且顯示<br /> gridview.setAdapter(saImageItems);<br /> //添加訊息處理<br /> gridview.setOnItemClickListener(new ItemClickListener());<br /> }</p><p> //當AdapterView被單擊(觸控螢幕或者鍵盤),則返回的Item單擊事件<br /> class ItemClickListener implements OnItemClickListener<br /> {<br />public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened<br /> View arg1,//The view within the AdapterView that was clicked<br /> int arg2,//The position of the view in the adapter<br /> long arg3//The row id of the item that was clicked<br /> ) {<br />//在本例中arg2=arg3<br />HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);<br />//顯示所選Item的ItemText<br />setTitle((String)item.get("ItemText"));<br />}</p><p> }