Android学习笔记之GridView
<1>简介
一个在平面上可显示多个条目的可滚动的视图组件,该组件中的条目通过一个ListAdapter和该组件进行关联。
比如实现九宫格图,用GridView是首选,也是最简单的。
<2>XML属性
属性:
android:columnWidth
设置列的宽度。关联的方法为:setColumnWidth(int)
android:gravity
设置此组件中的内容在组件中的位置。
必须是一个或多个(用“|”分隔开)以下的常量。
top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill
关联方法:setGravity (int gravity)
android:horizontalSpacing
两列之间的间距。关联方法:setHorizontalSpacing(int)
android:numColumns
列数。关联方法:setNumColumns(int)
可以是auto_fit
常量,表示尽可能多的显示列来填补的可用空间。
android:stretchMode
缩放模式。关联方法:setStretchMode(int)
Constant | Value | Description |
---|---|---|
none | 0 | 无 |
spacingWidth | 1 | 在每列之间的空间进行缩放 |
columnWidth | 2 | 每列进行平均的缩放 |
spacingWidthUniform | 3 | 每一列之间的间隔是均匀地缩放. |
android:verticalSpacing
两行之间的间距。关联方法:setVerticalSpacing(int)
<3>常用公共方法
public ListAdapter getAdapter()
获得与此组件相关的适配器
返回值: ListAdapter适配器实例
public int getNumColumns()
获得GridView的列数
public int getStretchMode()
获得GridView的缩放模式
public void setAdapter(ListAdapter adapter)
设置与此组件相关的适配器
public void setColumnWidth(int columnWidth)
设置GridView的列宽.
public void setGravity(int gravity)
设置控件内容的位置,默认值为:Gravity.LEFT
public void setHorizontalSpacing(int horizontalSpacing)
设置列间距.
public void setNumColumns(int numColumns)
设置grid的列数
public void setSelection(int position)
设置选中的条目.
public void setStretchMode(int stretchMode)
设置grid中的条目以什么缩放模式去填充空间。
stretchMode 可选值:NO_STRETCH,STRETCH_SPACING,STRETCH_SPACING_UNIFORM,或STRETCH_COLUMN_WIDTH
<4>范例
main.xml
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:verticalSpacing="5dip" android:horizontalSpacing="5dip" android:columnWidth="80dip" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:gravity="center" />
package xiaosi.gridview; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.Toast; public class GridViewActivity extends Activity { /** Called when the activity is first created. */ private GridView gridview = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridview = (GridView)findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(GridViewActivity.this)); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(GridViewActivity.this, "" + arg2, Toast.LENGTH_SHORT).show(); } }); } public class ImageAdapter extends BaseAdapter { private Context context; private int[] ImageBox ={ R.drawable.sample_1,R.drawable.sample_0, R.drawable.sample_3,R.drawable.sample_4, R.drawable.sample_5,R.drawable.sample_2, R.drawable.sample_3,R.drawable.sample_2, R.drawable.sample_4,R.drawable.sample_5, R.drawable.sample_4,R.drawable.sample_2, R.drawable.sample_3,R.drawable.sample_3, R.drawable.sample_4,R.drawable.sample_5, R.drawable.sample_5,R.drawable.sample_7}; public ImageAdapter(Context context){ this.context = context; } public int getCount() { return ImageBox.length; } public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(context); imageView.setLayoutParams(new GridView.LayoutParams(120, 120)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } imageView.setImageResource(ImageBox[position]); return imageView; } } }