我将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采用了二维表的方式显示,就需要设置行和列。设置列采用<GridView>的columnWidth属性。但是并不需要设置行数。
Adapter和ListView一样
下面直接看例子:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_horizontal">
<GridView android:id="@+id/gridview" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="4"
android:padding="20dp" android:horizontalSpacing="6dp"
android:verticalSpacing="6dp" />
<ImageView android:id="@+id/imageview" android:layout_width="fill_parent"
android:layout_height="150dp" />
</LinearLayout>
cell.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/imageview" android:layout_width="48dp"
android:layout_height="48dp" />
</LinearLayout>
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<Map<String, Object>> cells = new ArrayList<Map<String, Object>>();
for (int i = 0; i < resIds.length; i++)
{
Map<String, Object> cell = new HashMap<String, Object>();
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]);
}
}