Android控件详解之网格控件

我将Android控件的网格控件的学习知识总结一下和大家共享包括(GridView)

在Android开发中,罗列信息或者整理信息就是需要用到网格控件,Android源生提供了GridView控件。

1、GridView控件

GridView控件用于显示一个网格,实际上,和LIstView这一类的控件使用方法相似,只是在显示上的不同。

gridView的属性可以参考http://wenku.baidu.com/link?url=QzEL1ZQHXy3shjf1xjnQPFYLNw53Ed3phyBYpvk90fRcXPVq1Tmo97d_JNs2TqWuDFmO3Td_Dh7s-MYfxji6uXUsEYoQW39gR5PT1DKrGvq

或者http://blog.sina.com.cn/s/blog_604ea4410100wren.html

这边比较懒,没有整理罗列。

在这里GridView采用了二维表的方式显示,就需要设置行和列。设置列采用的columnWidth属性。但是并不需要设置行数。

Adapter和ListView一样

下面直接看例子:

main.xml



	 
	

cell.xml



	

java代码实现:

public class Main extends Activity implements OnItemSelectedListener,
		OnItemClickListener
{
	private ImageView imageView;
	private int[] resIds = new int[]
	{ R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4,
			R.drawable.item5, R.drawable.item6, R.drawable.item7,
			R.drawable.item8, R.drawable.item9, R.drawable.item10,
			R.drawable.item11, R.drawable.item12, R.drawable.item13,
			R.drawable.item14, R.drawable.item15, R.drawable.item16 };

	@Override
	public void onItemSelected(AdapterView parent, View view, int position,
			long id)
	{
		imageView.setImageResource(resIds[position]);
	}

	@Override
	public void onNothingSelected(AdapterView parent)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onItemClick(AdapterView parent, View view, int position,
			long id)
	{
		imageView.setImageResource(resIds[position]);

	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		GridView gridView = (GridView) findViewById(R.id.gridview);

		List> cells = new ArrayList>();
		for (int i = 0; i < resIds.length; i++)
		{
			Map cell = new HashMap();
			cell.put("imageview", resIds[i]);
			cells.add(cell);
		}
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, cells,
				R.layout.cell, new String[]
				{ "imageview" }, new int[]
				{ R.id.imageview });
		gridView.setAdapter(simpleAdapter);
		imageView = (ImageView) findViewById(R.id.imageview);
		gridView.setOnItemSelectedListener(this);
		gridView.setOnItemClickListener(this);
		imageView.setImageResource(resIds[0]);
	}
}


 

你可能感兴趣的:(知识总结)