Android GridView簡單實例
更新時間:2017年01月25日 09:56:46 作者:stevefat
這篇文章主要為大家詳細介紹了Android GridView簡單實例,簡單實現(xiàn)九宮格效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
也是今天用到的一個東西,就是簡單實現(xiàn)九宮格的Demo
1.就是定義各種layout 和對應的item
我的:
<?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:background="#fff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:background="#fff"></GridView>
</LinearLayout>
</LinearLayout>
itme的
<?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:padding="10dp"
android:layout_gravity="center"
android:background="#fff"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="管線" />
</LinearLayout>
開始準備數(shù)據(jù):
/**
* 準備顯示的數(shù)據(jù)
*/
public void initData() {
// 生成動態(tài)數(shù)組,并且轉入數(shù)據(jù) ,暫時就這樣來處理
lstImageItem = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < 3; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.osg);// 添加圖像資源的ID
map.put("ItemText", "各種管線" + String.valueOf(i));// 按序號做ItemText
lstImageItem.add(map);
}
}
設置顯示
gv = (GridView) view.findViewById(R.id.gridView);
SimpleAdapter adapter = new SimpleAdapter(this, lstImageItem, R.layout.gridview_item, new String[] { "ItemImage", "ItemText" },
new int[] { R.id.iv, R.id.tv });
gv.setAdapter(adapter);
最后扔一張效果圖

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- android中GridView實現(xiàn)點擊查看更多功能示例
- Android中控件GridView實現(xiàn)設置行列分割線的方法示例
- Android使用GridView實現(xiàn)日歷功能示例(詳細代碼)
- Android使用GridView實現(xiàn)日歷的簡單功能
- Android自定義DataGridView數(shù)據(jù)表格控件
- Android 利用ViewPager+GridView實現(xiàn)首頁導航欄布局分頁效果
- Android 中 GridView嵌套在ScrollView里只有一行的解決方法
- Android GridView實現(xiàn)動畫效果實現(xiàn)代碼
相關文章
Android中給fragment寫入?yún)?shù)的輕量開發(fā)包FragmentArgs簡介
這篇文章主要介紹了Android中給fragment寫入?yún)?shù)的輕量開發(fā)包FragmentArgs簡介,需要的朋友可以參考下2014-10-10
使用RoundedBitmapDrawable生成圓角圖片的方法
由于RoundedBitmapDrawable類沒有直接提供生成圓形圖片的方法,所以生成圓形圖片首先需要對原始圖片進行裁剪,將圖片裁剪成正方形,最后再生成圓形圖片,具體實現(xiàn)方法,可以參考下本文2016-09-09

