Android GridView 使用方法

来源:互联网 发布:迅捷网络哪里能下载 编辑:程序博客网 时间:2024/05/21 09:47
1. GridView的xml描述
<?xml version="1.0" encoding="utf-8"?>  
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:verticalSpacing="10dip"  
android:horizontalSpacing="10dip"  
android:gravity="center"  
android:numColumns="4" >

</GridView>

某些属性描述:
android:numColumns="auto_fit" ,GridView的列数设置为自动
android:columnWidth="90dp",每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth",缩放与列宽大小同步
android:verticalSpacing="10dp",两行之间的边距,如:行一(NO.0~NO.2)与行二(NO.3~NO.5)间距为10dp
android:horizontalSpacing="10dp",两列之间的边距。


2. GridView的子项描述:
grid_item.xml,这个XML跟描述每个gridview子项:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
android:layout_height="wrap_content"   
android:layout_width="match_parent"
android:paddingBottom="4dip" >  

<ImageView   
android:layout_height="100dip"   
android:layout_width="wrap_content"   
android:layout_centerHorizontal="true"
android:id="@+id/ItemImage">   
</ImageView>  

<TextView   
android:layout_width="wrap_content"   
android:layout_height="wrap_content"   
android:text="TextView01"
android:layout_centerHorizontal="true"   
android:layout_below="@+id/ItemImage"   
android:id="@+id/ItemText">  
</TextView>

</RelativeLayout>


3. Activity中的代码
//Activity.onCreate构建grid
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.home);

mInflater = LayoutInflater.from(this);
mMyView = mInflater.inflate(R.layout.home, null);
setContentView(mMyView);
mGrid = (GridView) mMyView.findViewById(R.id.gridView1);

//1. 生成动态数组
ArrayList<HashMap<String, Object>> lstGridItem = new ArrayList<HashMap<String, Object>>();  
for(int i=0; i<sIconArray.length ;i++)  
{  
HashMap<String, Object> map = new HashMap<String, Object>();  
map.put("ItemImage", sIconArray[i]);//添加图像资源的ID  
map.put("ItemText", "NO."+String.valueOf(i));//按序号做ItemText  
lstGridItem.add(map);  
}  

//2. 生成SimpleAdapter适配器
SimpleAdapter saImageItems = new SimpleAdapter(this,
lstGridItem,//数据来源   
R.layout.grid_item,//grid_item的XML实现  
//map中使用的key值列表
new String[] {"ItemImage", "ItemText"},   
//grid_Item.xml文件中每个item的UI元素ID  
new int[] {R.id.ItemImage, R.id.ItemText});
mGrid.setAdapter(saImageItems);

//3. 设置listener
mGrid.setOnItemClickListener(new ItemClickListener());
}

//当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件  
class  ItemClickListener implements OnItemClickListener  
{  
public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened   
View arg1,//The view within the AdapterView that was clicked  
int arg2,//The position of the view in the adapter  
long arg3//The row id of the item that was clicked  
) {  
//在本例中arg2=arg3  
HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);  
//显示所选Item的ItemText  
setTitle((String)item.get("ItemText"));
}
}  
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 中考准考证密码忘了怎么办 准考证号和密码忘了怎么办 安运继续教育考试不及格怎么办 微信的视频打不开怎么办 公众号被取消了怎么办 合同上不写工资怎么办 已经上班了想考个大专文凭怎么办 微信小程序违规暂停服务怎么办 程序锁密码忘了怎么办 忘了应用锁密码怎么办 毕业后发现论文有错误怎么办 柯丽尔打胶片时胶片卡住了怎么办 闽教英语要收费怎么办 高一学生英语差怎么办 高一英语基础差怎么办 老师教育学生学生跑出教室怎么办 研究生课题难出论文怎么办 一审过了上诉期怎么办 民事判决赔偿不给钱怎么办 民事申诉期过了怎么办 过了两年申诉期怎么办 微快递下单不能定位怎么办 网上打字兼职被骗了怎么办 微信银行卡转错怎么办 在支付宝被诈骗怎么办 发现被骗了报警不理怎么办 投稿投错了网站怎么办 六个月宝宝吃辅食便秘怎么办 六个月宝宝加辅食后便秘怎么办 婴儿6个月便秘怎么办 7个月的孩子便秘怎么办 四个月宝宝喜欢吃手怎么办 博瑞智教育是上当了怎么办 我43岁记忆力差怎么办 艾灸灸出的湿疹怎么办 饭店合同到期房东不租怎么办 极端暴力控制不住自己怎么办 苹果已停止访问该网页怎么办 qq登陆后隐藏了怎么办 易班密码忘记了怎么办 老师上课讲错了怎么办