GirdView
用于在界面上按照行列方式来显示多个组件
常用的三个属性(基本属性除外):
- ndroid:numColums 每一行显示几列
- android:horizontalSpacing 两列之间的距离
- android:verticalSpacing 两行之间的距离
example :
使用simpleAdapter 和 OnItemClickListenner 来实现
部分代码如下:
-----------------------------------------------------------------------------------------
/*
* 1.准备数据源
* 2.新建适配器(simpleAdapter)
* 3.GridView加载适配器
* 4.GridView配置事件监听器(onItemClickListenner)
*
*/
private GridView gv;
private List <Map<String,Object>> data;
private int []icon={R.drawable.icon1,R.drawable.icon2,R.drawable.icon3,
R.drawable.icon4,R.drawable.icon5,R.drawable.icon6,
R.drawable.icon7,R.drawable.icon8,R.drawable.icon9,};
private String[]name={"啊","哦","额","嗯","噶","吖","噢","喔","阿"};
private SimpleAdapter sim_adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView) findViewById(R.id.gridView);
data = new ArrayList<Map<String,Object>>();
sim_adapter = new SimpleAdapter(this, getdata(), R.layout.item, new String[]{"image","text"}, new int[]{R.id.imageView,R.id.textview});
gv.setAdapter(sim_adapter);
}
private List<Map<String,Object>> getdata() {
// TODO Auto-generated method stub
for(int i=0;i<9;i++){
Map<String,Object>map = new HashMap<String, Object>();
map.put("image", icon[i]);
map.put("text", name[i]);
data.add(map);
}
return data;
}
------------------------------------------------------------------------------------
至于实现 OnItemclickListenner 是和之前的Listview 基本是一样的原理
这两个组件相似度很高..