布局文件activity_main.xml
gridview_item.xml
这个是一个item的单元格样式的,有图片和文字
<&#63;xml version="1.0" encoding="utf-8"&#63;>
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); // 设置适配器的图片资源 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 }; // 设置标题 String[] title = new String[] { "相机", "定位", "画笔", "视频", "声音", "相机", "定位", "画笔", "视频", "声音", "相机", "定位", "画笔", "视频", "声音", "相机", "定位", "画笔", "视频", "声音" }; List
GridViewAdapter.java
这个是适配器
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> listitem; public GridViewAdapter(Context context,List > 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 map = listitem.get(position); imageView.setImageResource((Integer) map.get("image")); textView.setText(map.get("title") + ""); return convertView; } }
效果图如下:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接