GridView跟ListView都是比较常用的多控件布局,而GridView更是实现九宫图的首选!本文就是介绍如何使用GridView实现九宫图。GridView的用法很多,网上介绍最多的方法就是自己实现一个ImageAdapter继承BaseAdapter,再供GridView使用,类似这种的方法本文不再重复,本文介绍的GridView用法跟前文ListView的极其类似。。。。也算是我偷懒一下,嘻嘻嘻嘻。。。。
先来贴出本文代码运行的结果:
本文需要添加/修改3个文件:main.xml、night_item.xml、JAVA源代码。
main.xml源代码如下,本身是个GirdView,用于装载Item:
view plaincopy to clipboardprint?
<?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:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
<?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:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
介绍一下里面的某些属性:
android:numColumns="auto_fit" ,GridView的列数设置为自动
android:columnWidth="90dp",每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth",缩放与列宽大小同步
android:verticalSpacing="10dp",两行之间的边距,如:行一(NO.0~NO.2)与行二(NO.3~NO.5)间距为10dp
android:horizontalSpacing="10dp",两列之间的边距。
接下来介绍 night_item.xml,这个XML跟前面ListView的ImageItem.xml很类似:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip" android:layout_width="fill_parent">
<ImageView
android:layout_height="wrap_content"
android:id="@+id/ItemImage"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_below="@+id/ItemImage"
android:layout_height="wrap_content"
android:text="TextView01"
android:layout_centerHorizontal="true"
android:id="@+id/ItemText">
</TextView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip" android:layout_width="fill_parent">
<ImageView
android:layout_height="wrap_content"
android:id="@+id/ItemImage"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_below="@+id/ItemImage"
android:layout_height="wrap_content"
android:text="TextView01"
android:layout_centerHorizontal="true"
android:id="@+id/ItemText">
</TextView>
</RelativeLayout>
最后就是JAVA的源代码了,也跟前面的ListView的JAVA源代码很类似,不过多了“选中”的事件处理:
view plaincopy to clipboardprint?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
//生成动态数组,并且转入数据
ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
for(int i=0;i<10;i++)
分享到:
相关推荐
详细解说STL 详细解说STL 详细解说STL 详细解说STL
详细解说STL排序 详细解说STL排序 详细解说STL排序
专业的网络中心机房布局及设备(详细图文解说)
八段锦动作详细解说-精校版.doc
用户控件自定义控件 详细解说,想看的来下把,顺便给我混点分
VB控件属性大全,详细讲解VB的控件属性,图文解说。
java知识详细解说java知识详细解说java知识详细解说java知识详细解说java知识详细解说java知识详细解说java知识详细解说
对用VC编写IME输入法程序进行了详细解说。 输入法输入法
中文selinux手册和selinux详细解说,非常适合入门学习,很受用。
电话机 部分电路详细解说 超好PPT文件
C# 程序界面美化 ,更换程序皮肤,内含皮肤控件,和皮肤文件,和详细解说,让你做的程序达到专业级水平
电话机 部分电路详细解说 电话机 部分电路详细解说 电话机 部分电路详细解说
JVM结构详细解说图
青鸟电影院开发5、解说怎么通过代码绑定控件
BeanUtils详细解说BeanUtils详细解说BeanUtils详细解说BeanUtils详细解说BeanUtils详细解说
本文内容主要对三极管的工作原理进行解说,从三极管的结构讲起详细讲解三极管工作过程与放大原理等。 对三极管放大作用的理解,切记一点:能量不会无缘无故的产生,所以,三极管一定不会产生能量。但三极管厉害的...
delphi 扫雷 带源代码 详细解说 图片 大致算法 简单版 适合新手 此程序 我小弟自己遍的碍才500来行 有缺点欢迎指出
专业的网络中心机房布局及设备(详细图文解说)[汇编].pdf
电话机部分电路详细解电电话机部分电路详细解说话机部分电路详细解说说电话机部分电路详细解说
对于多中断部分进行了详细解说,适合新人上手和牛人参考。