【05】GridView初次使用
来源:互联网 发布:大数据设计方案 编辑:程序博客网 时间:2024/06/07 02:30
【一】效果图
【二】源程序
package min.gridview_1;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.GridView;import android.widget.SimpleAdapter;import android.os.Build;public class MainActivity extends Activity { private GridView gridView; private SimpleAdapter adapter; private List<Map<String, Object>> dataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); gridView=(GridView) findViewById(R.id.gridView); dataList=new ArrayList<Map<String,Object>>(); adapter=new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","name"}, new int[]{R.id.pic,R.id.name}); gridView.setAdapter(adapter); } private List<Map<String, Object>> getData() { int[] drawable = { 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 }; String[] iconName = { "联系人", "日历", "照相机", "时钟", "游戏", "短信", "铃声", "设置", "语音", "天气", "浏览器", "Youtube" }; for (int i = 0; i < drawable.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("pic", drawable[i]); map.put("name", iconName[i]); dataList.add(map); } Log.i("Main", "size="+dataList.size()); return dataList; }}
【三】布局代码
(1.1)main.xml
<?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="#000000" android:orientation="vertical" > <!-- GridView是可滚动的网格。一般用来显示多张图片。 android:horizontalSpacing="5dp" 两列之间的间距是5dp android:verticalSpacing="5dp" 两行之间的间距是5dp android:stretchMode="spacingWidth" 缩放与列宽大小同步 android:numColumns="auto_fit" 本来是一行显示几个,现在改为自动分配 --> <GridView android:id="@+id/gridView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:gravity="center_horizontal" android:horizontalSpacing="10dp" android:numColumns="3" android:verticalSpacing="10dp" /></LinearLayout>
(2)其中的item.xml布局
<?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/pic" android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="center" android:src="@drawable/address_book" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="5dp" android:text="名字" android:textColor="#ffffff" /></LinearLayout>
0 0
- 【05】GridView初次使用
- 初次使用
- 初次使用
- 初次使用
- 初次使用
- 初次使用
- 初次使用
- 初次使用BLOG
- 今天初次使用Blog...
- eclipse初次使用
- 初次使用virtual pc
- Ajax的初次使用
- 初次使用消息队列
- 初次使用Gdi+
- 初次使用CSDN博客
- SQLServer2005ReportingServices初次使用感受
- maven初次使用
- DWR-初次使用
- Java String 源代码分析
- PHP使用fopen()函数打开文件提示权限不够问题
- ScheduledExecutorService定时周期执行指定的任务
- 数据挖掘
- 字段冗余以提高查询效率问题
- 【05】GridView初次使用
- 九型人格的类型及分析
- 名片、文件扫描成图,图片剪切优化
- 二分图的最大匹配
- 软件测试一二章总结
- powerdesigner 总提示打印机服务
- 关于intellij14的web项目导入lib丢失以及上传资源图片等无法访问的问题
- proguard.cfg和proguard-project.txt 混淆文件的用法与配置
- Python Socket编程