国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

android中GridView的用法示例

2019-11-26 15:27:32
字體:
供稿:網(wǎng)友

在Android程序設(shè)計(jì)中GridView跟ListView都是比較常用的多控件布局,而GridView更是實(shí)現(xiàn)九宮圖的首選!本文就是介紹如何使用GridView實(shí)現(xiàn)九宮圖。GridView的用法很多,網(wǎng)上介紹最多的方法就是自己實(shí)現(xiàn)一個(gè)ImageAdapter繼承BaseAdapter,再供GridView使用,類似這種的方法本文不再重復(fù),本文介紹的GridView用法跟之前介紹過的ListView極其類似。

我們先來看看本文代碼運(yùn)行的結(jié)果:

本文需要添加/修改3個(gè)文件:main.xml、night_item.xml、JAVA源代碼。

main.xml源代碼如下,本身是個(gè)GirdView,用于裝載Item:

<?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"/>

這里簡單介紹一下里面的某些屬性:

android:numColumns="auto_fit" ,GridView的列數(shù)設(shè)置為自動(dòng)
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,這個(gè)XML跟前面ListView的ImageItem.xml很類似:

<?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>

最后就是JAVA的源代碼了,也跟前面的ListView的JAVA源代碼很類似,不過多了“選中”的事件處理:

public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);GridView gridview = (GridView) findViewById(R.id.gridview);//生成動(dòng)態(tài)數(shù)組,并且轉(zhuǎn)入數(shù)據(jù)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));//按序號(hào)做ItemText lstImageItem.add(map);}//生成適配器的ImageItem <====> 動(dòng)態(tài)數(shù)組的元素,兩者一一對(duì)應(yīng)SimpleAdapter saImageItems = new SimpleAdapter(this, //沒什么解釋     lstImageItem,//數(shù)據(jù)來源      R.layout.night_item,//night_item的XML實(shí)現(xiàn)          //動(dòng)態(tài)數(shù)組與ImageItem對(duì)應(yīng)的子項(xiàng)         new String[] {"ItemImage","ItemText"},           //ImageItem的XML文件里面的一個(gè)ImageView,兩個(gè)TextView ID     new int[] {R.id.ItemImage,R.id.ItemText});//添加并且顯示gridview.setAdapter(saImageItems);//添加消息處理gridview.setOnItemClickListener(new ItemClickListener());}//當(dāng)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")); }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泰来县| 北宁市| 阳山县| 麻栗坡县| 唐山市| 沿河| 斗六市| 博白县| 香格里拉县| 西丰县| 尤溪县| 扎鲁特旗| 漯河市| 湟中县| 寻甸| 和政县| 永川市| 崇州市| 安塞县| 英德市| 永清县| 邻水| 海林市| 鄂尔多斯市| 锡林郭勒盟| 蓬安县| 宜川县| 临夏市| 赤峰市| 宁海县| 黑水县| 琼海市| 扎囊县| 策勒县| 钦州市| 灵宝市| 新兴县| 筠连县| 四会市| 阿勒泰市| 小金县|