类概述 在二维滚动网格中显示条目的视图.网格中的条目来自与视图关联的 ListAdapter. XML属性
实例 布局文件 <?xml version="1.0" encoding="utf-8"?><GridViewxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center" /> mainActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this)); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show(); } }); } ImageAdapter public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } // create a new ImageView for each item referenced by the Adapter 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(mContext); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } imageView.setImageResource(mThumbIds[position]); return imageView; } // references to our images private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } 代码量很小,我就不上传程序了!给大家分享一个我在网上找的GridView和ListView的上拉加载,下拉刷新的例子吧(个人感觉很实用)! 又放不下了!郁闷中...... 详见的表格都在 史上最全系列之用户界面之gridview二 里了! |
<ignore_js_op>
-
效果图
附件表格
表格1
ConstConstantantValueDescDescriptionriptiontop0x30将对象放在其容器的顶部,不改变其大小.bottom0x50将对象放在其容器的底部,不改变其大小.left0x03将对象放在其容器的左侧,不改变其大小.right0x05将对象放在其容器的右侧,不改变其大小.center_vertical0x10将对象纵向居中,不改变其大小.fill_vertical0x70必要的时候增加对象的纵向大小,以完全充满其容器.center_horizontal0x01将对象横向居中,不改变其大小.fill_horizontal0x07必要的时候增加对象的横向大小,以完全充满其容器.center0x11将对象横纵居中,不改变其大小.fill0x77必要的时候增加对象的横纵向大小,以完全充满其容器.clip_vertical0x80附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容.剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.clip_horizontal0x08附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容.剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.start0x00800003将对象置于容器的开始处,不改变其大小.end0x00800005将对象置于容器的结尾处,不改变其大小.
表格2
ConConstantstantValValueueDescrDescriptioniptionauto_fit-1显示尽可能多的列,以填充所有空白.
表格3
ConstantConstant ValValue DescriptionDescription none 0 不伸缩. spacingWidth 1 伸缩各列间的空白. columnWidth 2 各列伸缩为等宽. spacingWidthUniform 3 各列空白均匀伸缩.