Android 控件GridView使用案例講解
Android:控件GridView的使用
如果是列表(單列多行形式)的使用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"-------水平邊距
- 準(zhǔn)備數(shù)據(jù)源
- 新建適配器
- 加載適配器
GridView(網(wǎng)格視圖)是按照行列的方式來顯示內(nèi)容的,一般用于顯示圖片,圖片等內(nèi)容,比如實(shí)現(xiàn)九宮格圖,用GridView是首選,也是最簡單的,下面來個(gè)實(shí)例,
效果圖:

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的長度是相同的,這里任選其一都可以
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;
}
}
<?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
到此這篇關(guān)于Android 控件GridView使用案例講解的文章就介紹到這了,更多相關(guān)Android 控件GridView使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android?Spinner和GridView組件的使用示例
- Android使用GridView實(shí)現(xiàn)表格分割線效果
- Android自定義gridView仿頭條頻道拖動(dòng)管理功能
- Android開發(fā)之組件GridView簡單使用方法示例
- Android中GridView插件的使用方法
- Android控件gridview實(shí)現(xiàn)單行多列橫向滾動(dòng)效果
- Android通過實(shí)現(xiàn)GridView的橫向滾動(dòng)實(shí)現(xiàn)仿京東秒殺效果
- Android使用Gridview單行橫向滾動(dòng)顯示
- Android Gridview布局出現(xiàn)滾動(dòng)條或組件沖突解決方法
相關(guān)文章
android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android實(shí)現(xiàn)EditText圖文混合插入上傳功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)EditText圖文混合插入上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android使用 Coroutine + Retrofit打造簡單的HTTP請(qǐng)求庫
這篇文章主要介紹了Android使用 Coroutine + Retrofit打造簡單的HTTP請(qǐng)求庫,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Android編程實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能,結(jié)合具體實(shí)例形式分析了Android實(shí)現(xiàn)帶有單選按鈕的dialog對(duì)話框及帶有復(fù)選按鈕的dialog對(duì)話框相關(guān)操作技巧,需要的朋友可以參考下2017-09-09

