Android中GridView插件的使用方法
布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="16dp"
android:numColumns="4"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
gridview_item.xml
這個(gè)是一個(gè)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:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView" />
</LinearLayout>
MainActivity.java
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;
public class MainActivity extends Activity {
private GridView gridView;
private GridViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 設(shè)置適配器的圖片資源
int[] imageId = new int[] { R.drawable.chat_tool_camera,
R.drawable.chat_tool_location, R.drawable.chat_tool_paint,
R.drawable.chat_tool_video, R.drawable.chat_tool_voice,
R.drawable.chat_tool_camera, R.drawable.chat_tool_location,
R.drawable.chat_tool_paint, R.drawable.chat_tool_video,
R.drawable.chat_tool_voice, R.drawable.chat_tool_camera,
R.drawable.chat_tool_location, R.drawable.chat_tool_paint,
R.drawable.chat_tool_video, R.drawable.chat_tool_voice,
R.drawable.chat_tool_camera, R.drawable.chat_tool_location,
R.drawable.chat_tool_paint, R.drawable.chat_tool_video,
R.drawable.chat_tool_voice };
// 設(shè)置標(biāo)題
String[] title = new String[] { "相機(jī)", "定位", "畫筆", "視頻", "聲音", "相機(jī)",
"定位", "畫筆", "視頻", "聲音", "相機(jī)", "定位", "畫筆", "視頻", "聲音", "相機(jī)",
"定位", "畫筆", "視頻", "聲音" };
List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
// 將上述資源轉(zhuǎn)化為list集合
for (int i = 0; i < title.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", imageId[i]);
map.put("title", title[i]);
listitem.add(map);
}
adapter = new GridViewAdapter(MainActivity.this, listitem);
gridView = (GridView) this.findViewById(R.id.gridView);
gridView.setAdapter(adapter);
}
}
GridViewAdapter.java
這個(gè)是適配器
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewAdapter extends BaseAdapter {
private Context context;
private List<Map<String, Object>> listitem;
public GridViewAdapter(Context context,List<Map<String, Object>> listitem) {
this.context = context;
this.listitem = listitem;
}
@Override
public int getCount() {
return listitem.size();
}
@Override
public Object getItem(int position) {
return listitem.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
TextView textView = (TextView) convertView.findViewById(R.id.textView);
Map<String, Object> map = listitem.get(position);
imageView.setImageResource((Integer) map.get("image"));
textView.setText(map.get("title") + "");
return convertView;
}
}
效果圖如下:

總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Android?Spinner和GridView組件的使用示例
- Android 控件GridView使用案例講解
- Android使用GridView實(shí)現(xiàn)表格分割線效果
- Android自定義gridView仿頭條頻道拖動(dòng)管理功能
- Android開發(fā)之組件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 動(dòng)畫之AlphaAnimation應(yīng)用詳解
本節(jié)講解AlphaAnimation 動(dòng)畫,窗口的動(dòng)畫效果,淡入淡出什么的,有些游戲的歡迎動(dòng)畫,logo的淡入淡出效果就使用AlphaAnimation,具體的祥看本文,需要的朋友可以參考下2012-12-12
Android webveiw 出現(xiàn)棧錯(cuò)誤解決辦法
這篇文章主要介紹了Android webveiw 出現(xiàn)棧錯(cuò)誤解決辦法的相關(guān)資料,出現(xiàn)java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in privileged processes,這里提供解決辦法,需要的朋友可以參考下2017-08-08
Android中Item實(shí)現(xiàn)點(diǎn)擊水波紋效果
這篇文章主要給大家介紹了關(guān)于Android中Item實(shí)現(xiàn)點(diǎn)擊水波紋效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Android Studio中導(dǎo)入module的方法(簡單版)
這篇文章主要介紹了AndroidStudio中導(dǎo)入module的方法,本文是一篇簡易版的教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01
ViewPager打造輪播圖Banner/引導(dǎo)頁Guide
這篇文章主要為大家詳細(xì)介紹了ViewPager打造輪播圖Banner和引導(dǎo)頁Guide,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android中ListView下拉刷新的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Android中ListView下拉刷新的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-03-03
Android基于zxing的二維碼(網(wǎng)格)掃描 仿支付寶網(wǎng)格掃描
這篇文章主要為大家詳細(xì)介紹了Android基于zxing的二維碼網(wǎng)格掃描,仿支付寶網(wǎng)格掃描,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

