android中GridView的用法示例
在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用法跟之前介紹過(guò)的ListView極其類似。
我們先來(lái)看看本文代碼運(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" />
這里簡(jiǎn)單介紹一下里面的某些屬性:
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",兩列之間的邊距。
接下來(lái)介紹 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源代碼很類似,不過(guò)多了“選中”的事件處理:
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, //沒(méi)什么解釋 lstImageItem,//數(shù)據(jù)來(lái)源 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")); } }
- Android 組件Gallery和GridView示例講解
- Android中GridView插件的使用方法
- Android控件gridview實(shí)現(xiàn)單行多列橫向滾動(dòng)效果
- Android GridView擴(kuò)展仿微信微博發(fā)圖動(dòng)態(tài)添加刪除圖片功能
- Android開(kāi)發(fā)實(shí)現(xiàn)橫向列表GridView橫向滾動(dòng)的方法【附源碼下載】
- android ListView和GridView拖拽移位實(shí)現(xiàn)代碼
- Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法
- Android開(kāi)發(fā)之使用GridView展示圖片的方法
- Android GridView仿微信朋友圈顯示圖片
- Android開(kāi)發(fā)之組件GridView簡(jiǎn)單使用方法示例
相關(guān)文章
java 獲取數(shù)據(jù)庫(kù)連接的實(shí)現(xiàn)代碼
本篇文章是對(duì)在java中獲取數(shù)據(jù)庫(kù)連接的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05MyBatis中criteria的or(或查詢)語(yǔ)法說(shuō)明
這篇文章主要介紹了MyBatis中criteria的or(或查詢)語(yǔ)法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Springboot?JPA如何使用distinct返回對(duì)象
這篇文章主要介紹了Springboot?JPA如何使用distinct返回對(duì)象,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java?ServletContext與ServletConfig接口使用教程
ServletConfig對(duì)象,叫Servlet配置對(duì)象。主要用于加載配置文件的初始化參數(shù)。我們知道一個(gè)Web應(yīng)用里面可以有多個(gè)servlet,如果現(xiàn)在有一份數(shù)據(jù)需要傳給所有的servlet使用,那么我們就可以使用ServletContext對(duì)象了2022-09-09SpringBoot使用責(zé)任鏈模式優(yōu)化業(yè)務(wù)邏輯中的if-else代碼
在開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)遇到需要根據(jù)不同的條件執(zhí)行不同的邏輯的情況,我們可以考慮使用責(zé)任鏈模式來(lái)優(yōu)化代碼結(jié)構(gòu),使得代碼更加清晰、可擴(kuò)展和易于維護(hù)2023-06-06Eclipse中安裝反編譯工具Fernflower的方法(Enhanced Class Decompiler)
這篇文章主要介紹了Eclipse中安裝反編譯工具Fernflower的方法(Enhanced Class Decompiler),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Java數(shù)組聲明、創(chuàng)建、初始化基礎(chǔ)
本文講述了Java數(shù)組的幾個(gè)相關(guān)的方面,講述了對(duì)Java數(shù)組的聲明、創(chuàng)建和初始化,并給出其對(duì)應(yīng)的代碼2012-12-12