GridView控件是可以用来显示二维排列的控件,这里在上一篇TabHost控件的基础上添加了一个GridView控件,用作Tab页的显示内容。

效果图:

帖代码:

public class OrderClientActivity extends Activity {TabHost tabHost;String[] dishType=new String[]{"1","2","3","4","5","6","7"};GridView gridView;String[] dishName=new String[]{"1","2","3","4","5","6","7","8","9","10","11","12"};String picPath=Environment.getExternalStorageDirectory().getPath() + "/picture/";GridViewAdapter gridViewAdapter;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.orderform);// 获取TabHost对象TabHost tabHost = (TabHost) findViewById(R.id.tabhost);// 如果没有继承TabActivity时,通过该种方法加载启动tabHosttabHost.setup();gridView=(GridView)findViewById(R.id.view1);gridViewAdapter=new GridViewAdapter(this);gridView.setAdapter(gridViewAdapter);for(int i=0;i<dishType.length;i++){TabHost.TabSpec tabSpec=tabHost.newTabSpec(dishType[i]);tabSpec.setIndicator(dishType[i],getResources().getDrawable(R.drawable.ic_launcher));tabSpec.setContent(R.id.view1);tabHost.addTab(tabSpec);}tabHost.setCurrentTab(1);tabHost.setCurrentTab(0);new AsyncLoadImage().execute(100);}class AsyncLoadImage extends AsyncTask<Object, Bitmap, Object>{@Overrideprotected Object doInBackground(Object... params) {Bitmap bitmap;Log.i("加载线程", "");// TODO Auto-generated method stubfor(int i=0;i<dishName.length;i++){Log.i("图片路径", picPath + dishName[i] + ".jpg");bitmap=BitmapFactory.decodeFile(picPath + dishName[i] + ".jpg");if(bitmap != null){publishProgress(bitmap);Log.i("图片解析", "正确");}elseLog.i("图片解析", "错误");}return null;}@Overrideprotected void onProgressUpdate(Bitmap... values) {// TODO Auto-generated method stubfor(Bitmap b : values){gridViewAdapter.addPic(b);gridViewAdapter.notifyDataSetChanged();}}}class GridViewAdapter extends BaseAdapter{Context context;private ArrayList<Bitmap> picList=new ArrayList<Bitmap>();GridViewAdapter(Context context){this.context=context;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn picList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn picList.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}public void addPic(Bitmap bitmap){picList.add(bitmap);}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubLog.i("得到view", position + "");ImageView image = null;if(convertView == null){image=new ImageView(context);}else{image=(ImageView)convertView;}image.setLayoutParams(new GridView.LayoutParams((getWindowManager().getDefaultDisplay().getWidth()-3)/2,(int) (getWindowManager().getDefaultDisplay().getWidth()/2*0.6)));//设置ImageView对象布局 image.setAdjustViewBounds(true);//设置边界对齐 image.setScaleType(ImageView.ScaleType.FIT_XY);//设置刻度的类型 image.setPadding(0, 0, 0, 0);//设置间距 image.setImageBitmap(picList.get(position));return image;}}
}

GridView需要通过Adapter来加载需要显示的内容,在这里我们自定义了BaseAdapter,必须实现几个方法,其中getCount()和getView()是必须实现的方法,getView()用来提供GridView显示的每一个Item。关于Adapter的详细情况可以参考  Android之Adapter用法总结。

另外,实际的运行过程中,发现Gridview的图片加载很卡,所以我们改成了异步加载图片,用到了AsyncTask类,这是Android提供的一个轻量级的线程既能在后台线程中执行一些耗时操作,又有方法直接更新ui,关于AsyncTask的详细介绍可以参考:android AsyncTask介绍。

这里如果没有必要用到异步加载,可以在GridViewAdapter里直接给picList赋值。

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/hometabs"android:orientation="vertical"android:layout_width="fill_parent"  android:layout_height="fill_parent"><!-- TabHost必须包含一个 TabWidget和一个FrameLayout--> <TabHost android:id="@+id/tabhost"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"><LinearLayoutandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><HorizontalScrollViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:scrollbars="none"><!-- TabWidget的id属性必须为 @android:id/tabs-->            <TabWidget android:id="@android:id/tabs" android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"></TabWidget></HorizontalScrollView><!-- FrameLayout的id属性必须为 @android:id/tabcontent--><FrameLayout android:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"><GridView android:id="@+id/view1"android:listSelector="#000000"android:layout_width="fill_parent"android:layout_height="wrap_content"android:padding="0dip"android:layout_margin="0dip"android:numColumns="2"android:horizontalSpacing="3dip"android:verticalSpacing="3dip"android:stretchMode="columnWidth"android:gravity="fill"/>"</FrameLayout></LinearLayout></TabHost><LinearLayout android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="hha"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="hha2"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="hha3"/></LinearLayout></LinearLayout>

在布局文件里看到,GridView设置了一个属性android:listSelector,这个属性用来设置GridView的每个item 的背景。如果没有设置这个属性,每个item在点击的时候都有一个蓝色边框,我们可以设置成我们想要的颜色,不想显示的话可以直接设置成背景色。

下一篇介绍GridView的更复杂的用法。

Android的GridView控件相关推荐

  1. android studio 画控件,Android Studio 基础控件使用

    TextView android:gravity="center" //文字对其方式 top bottom left right center android:textColor= ...

  2. android中翻页控件,Android GridView控件分页自定义

    上一篇:Android GridView控件自定义中,我们自定义了Android GridView控件. 包名解释: com.yaomei.activity.adapter   DEMO使用到的自定义 ...

  3. android卡片风格,[Android] Android 卡片式控件CardView的优雅使用

    [Android] Android 卡片式控件CardView的优雅使用 CardView是在安卓5.0提出的卡片式控件 其具体用法如下: 1.在app/build.gradle 文件中添加 comp ...

  4. Android 常见界面控件(ListView、RecyclerView、自定义View篇)

    Android 常见界面控件(ListView.RecyclerView.自定义View篇) 目录 3.3 ListView的使用 3.3.1 ListView控件的简单使用 3.3.2 常用数据适配 ...

  5. Android活动,控件,碎片,广播,数据库小总结

    系统架构与开发环境搭建 3月2日Android Activities代码练习 3月5日 Intent的应用与传值 3月9日 1:显式Intent与隐式Intent的区别         显式Inten ...

  6. Android使用ListView控件问题

    Android使用ListView控件问题: The application has stopped unexpectedly, please try again. 开发环境:android 1.6 ...

  7. ASP.NET2.0中用Gridview控件操作数据

    在ASP.NET 2.0中,加入了许多新的功能和控件,相比asp.net 1.0/1.1,在各方面都有了很大的提高.其中,在数据控件方面,增加了不少控件,其中的Gridview控件功能十分强大.在本文 ...

  8. GridView控件修改、删除示例(修改含有DropDownList控件)

    GridView控件修改.删除例子,修改时含有DropDownList控件. 示例运行效果图: GridViewUp.aspx文件代码: <%@ Page Language="C#&q ...

  9. Android的WebView控件载入网页显示速度慢的究极解决方案

    Android的WebView控件载入网页显示速度慢的究极解决方案 [转载来源自http://hi.baidu.com/goldchocobo/] Android客户端中混搭HTML页面,会出现虽然H ...

最新文章

  1. 源代码从 300 行到 172,000 行,它用了 23 年
  2. at24c16如何划分出多个读写区_如何1分钟遍历100T数据?
  3. 10没有基于策略的qos_分布式QoS算法解析
  4. 机器人操作系统ROS Indigo 入门学习(1)——安装ROS Indigo【转】
  5. java练习:模拟试下你斗地主的洗牌、发牌、看牌功能
  6. node学习笔记--模块加载
  7. 用J-Link烧写u-boot到Nor Flash—— 韦东山嵌入式Linux视频学习笔记04
  8. How to create a Python dictionary with double quotes as default quote format?
  9. 鸿蒙os系统被推送,鸿蒙来了!华为大规模推送鸿蒙OS系统,造成网站一度瘫痪...
  10. 首个开源 Linux 系统登陆火星,占有率超 Windows,一同登录还有一款安卓手机芯片...
  11. c语言随机迷宫生成器,作为新手该如何快速上手 C++
  12. oracle对查询结果求和_oracle基础知识分享
  13. 使用npm失败解决方案
  14. 加密解密概述及openssl应用及其创建CA和签发证书的实现
  15. 【数位DP】恨7不成妻
  16. 判断wifi连接是否可用
  17. Linux系统监控命令整理汇总-掌握CPU,内存,磁盘IO等找出性能瓶颈
  18. SQLPub免费的MySQL数据库
  19. Excel合并多列增加指定字符指定字符替换为换行符调整行高步骤
  20. 管桩的弹性模量计算公式_弹性模量法测定桩身应力分析

热门文章

  1. 欧尼酱讲JVM(23)——垃圾收集器
  2. 企业单位通配符证书,通配符ov证书,泛域名ssl证书
  3. 计算机英语(61-90)
  4. 实训项目计算机基础知识实训报告,计算机应用基础(本)形考作业实训2学习实训报告.docx...
  5. 云桌面eyeOS之现状
  6. SAP会计基础知识(会计借贷关系)
  7. 智慧校园一卡通全场景解决方案(附原版PPT下载)
  8. 路由策略Routing Policy和策略路由PBR的区别
  9. 如何解决version `GLIBCXX_3.4.29‘ not found的问题
  10. MRP专题三:物料主数据MRP参数解释