Android:控件GridView的使用實(shí)例
如果是列表(單列多行形式)的使用ListView,如果是多行多列網(wǎng)狀形式的優(yōu)先使用GridView。
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > </GridView>
GirdView的一些屬性:
- android:numColumns="auto_fit" --------列數(shù)設(shè)置為自動(dòng)
- android:columnWidth="90dp",----------每列的寬度,也就是Item的寬度
- android:stretchMode="columnWidth"------縮放與列寬大小同步
- android:verticalSpacing="10dp"----------垂直邊距
- android:horizontalSpacing="10dp"-------水平邊距
1、準(zhǔn)備數(shù)據(jù)源
2、新建適配器
3、加載適配器
GridView(網(wǎng)格視圖)是按照行列的方式來顯示內(nèi)容的,一般用于顯示圖片,圖片等內(nèi)容,比如實(shí)現(xiàn)九宮格圖,用GridView是首選,也是最簡(jiǎn)單的,下面來個(gè)實(shí)例,
實(shí)例下載:demo
效果圖:
MainActivity.java
package com.example.testgridview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.GridView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private GridView gview; private List<Map<String, Object>> data_list; private SimpleAdapter sim_adapter; // 圖片封裝為一個(gè)數(shù)組 private int[] icon = { R.drawable.address_book, R.drawable.calendar, R.drawable.camera, R.drawable.clock, R.drawable.games_control, R.drawable.messenger, R.drawable.ringtone, R.drawable.settings, R.drawable.speech_balloon, R.drawable.weather, R.drawable.world, R.drawable.youtube }; private String[] iconName = { "通訊錄", "日歷", "照相機(jī)", "時(shí)鐘", "游戲", "短信", "鈴聲", "設(shè)置", "語音", "天氣", "瀏覽器", "視頻" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); gview = (GridView) findViewById(R.id.gview); //新建List data_list = new ArrayList<Map<String, Object>>(); //獲取數(shù)據(jù) getData(); //新建適配器 String [] from ={"image","text"}; int [] to = {R.id.image,R.id.text}; sim_adapter = new SimpleAdapter(this, data_list, R.layout.item, from, to); //配置適配器 gview.setAdapter(sim_adapter); } public List<Map<String, Object>> getData(){ //cion和iconName的長(zhǎng)度是相同的,這里任選其一都可以 for(int i=0;i<icon.length;i++){ Map<String, Object> map = new HashMap<String, Object>(); map.put("image", icon[i]); map.put("text", iconName[i]); data_list.add(map); } return data_list; } }
test.xml
<?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="match_parent" android:orientation="vertical" android:background="#000" > <GridView android:id="@+id/gview" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="auto_fit" android:columnWidth="80dp" android:stretchMode="columnWidth" ></GridView> </LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:padding="10dp" > <ImageView android:src="@drawable/ic_launcher" android:id="@+id/image" android:layout_width="60dp" android:layout_height="60dp" /> <TextView android:id="@+id/text" android:layout_marginTop="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:text="文字" /> </LinearLayout>
監(jiān)聽接口: onItemClickListener
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中實(shí)現(xiàn)多行、水平滾動(dòng)的分頁的Gridview實(shí)例源碼
- Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法
- android GridView多選效果的實(shí)例代碼
- android中GridView的用法示例
- Android實(shí)現(xiàn)九宮格(GridView中各項(xiàng)平分空間)的方法
- Android實(shí)現(xiàn)GridView中ImageView動(dòng)態(tài)變換的方法
- Android開發(fā)之實(shí)現(xiàn)GridView支付寶九宮格
- Android開發(fā)之使用GridView展示圖片的方法
- Android GridView仿微信朋友圈顯示圖片
- Android控件之GridView用法實(shí)例分析
- Android中在GridView網(wǎng)格視圖上實(shí)現(xiàn)item拖拽交換的方法
相關(guān)文章
Android開發(fā)中計(jì)算器的sin、cos及tan值計(jì)算問題分析
這篇文章主要介紹了Android開發(fā)中計(jì)算器的sin、cos及tan值計(jì)算問題,結(jié)合實(shí)例形式分析了Android三角函數(shù)運(yùn)算中的弧度與角度計(jì)算問題與相關(guān)解決方法,需要的朋友可以參考下2017-11-11Android進(jìn)階從字節(jié)碼插樁技術(shù)了解美團(tuán)熱修復(fù)實(shí)例詳解
這篇文章主要為大家介紹了Android進(jìn)階從字節(jié)碼插樁技術(shù)了解美團(tuán)熱修復(fù)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android手機(jī)鬧鐘服務(wù)AlarmManagerk開發(fā)案例
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)鬧鐘服務(wù)AlarmManagerk開發(fā)案例的資料,需要的朋友可以參考下2016-05-05Android開發(fā)實(shí)現(xiàn)的圓角按鈕、文字陰影按鈕效果示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的圓角按鈕、文字陰影按鈕效果,涉及Android界面布局與屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Android Canvas方法總結(jié)最全面詳解API(小結(jié))
本篇文章主要介紹了Android Canvas方法總結(jié)最全面詳解API(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Android 實(shí)現(xiàn)搶購(gòu)倒計(jì)時(shí)功能的示例
這篇文章主要介紹了Android 實(shí)現(xiàn)搶購(gòu)倒計(jì)時(shí)功能的示例,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-03-03Android開發(fā)實(shí)現(xiàn)讀取excel數(shù)據(jù)并保存為xml的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)讀取excel數(shù)據(jù)并保存為xml的方法,涉及Android針對(duì)Excel數(shù)據(jù)讀取及xml格式文件的構(gòu)造與保存相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android Studio控制臺(tái)出現(xiàn)中文亂碼(方框)問題解決辦法
這篇文章主要介紹了Android Studio控制臺(tái)出現(xiàn)中文亂碼(方框)問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06