Android中GridView插件的使用方法
程序员文章站
2022-07-04 20:20:12
布局文件activity_main.xml
布局文件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
这个是一个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); // 设置适配器的图片资源 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<map<string, object>> listitem = new arraylist<map<string, object>>(); // 将上述资源转化为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
这个是适配器
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; } }
效果图如下:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接