Android应用开发中使用GridView网格布局的代码示例
作者:泽江08 发布时间:2023-11-09 20:23:36
标签:Android,GridView
基本布局演示
1. 定义包含GridView 的 main.xmk
<?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"
>
<GridView
android:id="@+id/gride"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<strong> android:numColumns="3"</strong>
android:verticalSpacing="5dip"
/>
</LinearLayout>
这行应该注意一下:
android:numColumns="3"
用来设定GridView每行显示的View数目 如果没有这行 会默认每行显示一个View 和ListView 的一样
2. 自定义 class ImageList extends BaseAdapter 其中主要是:
View getView(int position, View convertView, ViewGroup parent)
用于显示目标ImageView
public class ImageList extends BaseAdapter {
Activity activity;
//construct
public ImageList(Activity a ) {
activity = a;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return image.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return image[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv = new ImageView(activity);
iv.setImageResource(image[position]);
return iv;
}
}
3. 给GridView指定Adapter
GridView gv = (GridView) findViewById(R.id.gride);
ImageList adapter = new ImageList(this);
gv.setAdapter(adapter);
所以最后效果图是这样的
巧妙地添加GridView的 网格线
ListView 中设置分隔线的加如下参数即可:
android:divider="@color/gray"
android:dividerHeight="1dp"
GridView网格布局,默认情况下是没有网格线的
查找网上资料,找到了一种为GridView添加网格线的小技巧
实际上,该网格线是通过设置GridView各子项的间隔,并分别设置GridView背景色与子项背景色实现的。
实现方法:
(1)设置GridView背景色,设置水平间方向间隔属性值android:horizontalSpacing和竖直方向间隔属性值
android:verticalSpacing
(2)设置GridView子项背景色
示例代码:
1.main.xml
<GridView
android:id="@+id/gv_words"
android:visibility="gone"
android:background="@color/gray"
android:columnWidth="60dp"
android:numColumns="5"
android:listSelector="@null"
android:verticalSpacing="1.0px"
android:horizontalSpacing="1.0px"
android:soundEffectsEnabled="true"
android:smoothScrollbar="true"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/llayout2"
android:layout_below="@+id/llayout1"/>
2.GrivViewItem布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" >
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp">
<TextView
android:id="@+id/gv_bushou_TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ItemImage"
android:layout_margin="1dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_above="@+id/gv_bushou_TextView2"
android:layout_toLeftOf="@+id/gv_bushou_TextView2"
android:textSize="25dp"
android:textColor="@color/blue"
android:text="难" >
</TextView>
<TextView
android:id="@+id/gv_bushou_TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ItemImage"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textColor="@color/gray"
android:textSize="10dp"
android:text="1笔" >
</TextView>
</RelativeLayout>
</LinearLayout>
3.运行截图
0
投稿
猜你喜欢
- Spring @Cacheable指定失效时间新版本配置@Configuration@EnableCachingpublic class R
- 这篇文章主要介绍了Spring 自动装配的二义性实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 本文实例讲述了C#编程获取各种电脑硬件信息的方法。分享给大家供大家参考,具体如下:获取CPU编号:ManagementClass mc =
- 本文是精讲RestTemplate第6篇,前篇的blog访问地址如下:RestTemplate在Spring或非Spring环境下使用精讲R
- 把SpringBoot项目打包成Docker镜像有两种方案:全自动化:先打好docker镜像仓库,然后在项目的maven配置中配置好仓库的地
- 在你的jar文件当前目录中建立一个bat文件:内容是:注意文件名要对应@echo offSTART "commandServer&
- 一、堆栈内存堆栈内存,顾名思义,指的是堆内存以及栈内存,其中,堆内存是由Java GC进行管理的内存区域,而栈内存则是线程内存。关于栈内存,
- 为了表示不同的浓度值,对颜色条应用颜色梯度变化,基本方法是对ARGB分量乘以一个渐变系数。下面是对十种颜色应用的三个梯度值的过程。 publ
- 本文实例讲述了Java编程中文件读写的方法。分享给大家供大家参考,具体如下:Java中文件读写操作的作用是什么?回答这个问题时应该先想到的是
- 在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启动的,而不需要另
- 前言你一定会好奇:“老周,你去哪开飞机了?这么久没写博客了。”老周:“我买不起飞机,开了个铁矿,挖了一年半的石头。谁知铁矿垮了,压死了几条蜈
- Bat.aspx: 程序代码 <%@ Page Language="C#" AutoEventWireu
- 1.Hibernate中的多表查询 1.1SQL中的多表查询【交叉连接】select * from A,B;【内连接】显示内连接:
- 状态机机制状态机机制是一种常用的解决状态扭转问题的方法,通过定义状态以及状态之间的转移规则来控制状态的流转。对于订单系统,我们可以使用状态机
- 这篇文章主要介绍了java property配置文件管理工具框架过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- 基本概念servlet 关系servlet 和 servletconfig 是一对一的关系;servletconfig 作用它存储 web.
- 前言JVM是Java中比较难理解和掌握的一部分,也是面试中被问的比较多的,掌握好JVM底层原理有助于我们在开发中写出效率更高的代码,可以让我
- 调用SAP WebService服务需要转换操作1、通过浏览器访问SAP WebService地址,进行验证并生成wsdl文件地址并不是可以
- 概述基于java + swing + JFrame 的图书馆管理系统,租车,还车,管理员管理用户,付款等。部分代码public class
- Semaphore、SemaphoreSlim 类两者都可以限制同时访问某一资源或资源池的线程数。这里先不扯理论,我们从案例入手,通过示例代