Android中选项卡功能的实现

Android中使用TabHostTabWidget来实现选项卡功能。TabHost必须是布局的根节点,它包含两个子节点:

TabWidget,显示选项卡;

FrameLayout,显示标签内容。

实现选项卡功能有两种方法,一种是将多个View放在同一个Activity中,然后使用使用标签来进行切换。另一种是直接使用标签切换不同的Activity。

后一种方法更为常用一些。本文也只介绍了后一种方法的实现过程。

1. 创建一个工程,名字可以叫HelloTabWidget。

2. 创建多个不同的Activity,用来表示各个标签页中的不同内容。

3. 为标签设计不同的icon。每个标签应该有两个icon,一个表示选中,一个未选中。将图片放在 res/drawable/文件夹下。然后创建一个相应的

StateListDrawable,用来实现在选中和未选中直接自动的切换。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- When selected, use grey -->
  4. <item android:drawable="@drawable/ic_tab_artists_grey"
  5. android:state_selected="true" />
  6. <!-- When not selected, use white-->
  7. <item android:drawable="@drawable/ic_tab_artists_white" />
  8. </selector>

4. 将main.xml替换为以下内容。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:padding="5dp">
  11. <TabWidget
  12. android:id="@android:id/tabs"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content" />
  15. <FrameLayout
  16. android:id="@android:id/tabcontent"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:padding="5dp" />
  20. </LinearLayout>
  21. </TabHost>

5. 让你的主Activity继承自TabActivity

6. 在主Activity的onCreate方法中加入标签

  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.main);
  4. Resources res = getResources(); // Resource object to get Drawables
  5. TabHost tabHost = getTabHost();  // The activity TabHost
  6. TabHost.TabSpec spec;  // Resusable TabSpec for each tab
  7. Intent intent;  // Reusable Intent for each tab
  8. // Create an Intent to launch an Activity for the tab (to be reused)
  9. intent = new Intent().setClass(this, ArtistsActivity.class);
  10. // Initialize a TabSpec for each tab and add it to the TabHost
  11. spec = tabHost.newTabSpec("artists").setIndicator("Artists",
  12. res.getDrawable(R.drawable.ic_tab_artists))
  13. .setContent(intent);
  14. tabHost.addTab(spec);
  15. // Do the same for the other tabs
  16. intent = new Intent().setClass(this, AlbumsActivity.class);
  17. spec = tabHost.newTabSpec("albums").setIndicator("Albums",
  18. res.getDrawable(R.drawable.ic_tab_albums))
  19. .setContent(intent);
  20. tabHost.addTab(spec);
  21. intent = new Intent().setClass(this, SongsActivity.class);
  22. spec = tabHost.newTabSpec("songs").setIndicator("Songs",
  23. res.getDrawable(R.drawable.ic_tab_songs))
  24. .setContent(intent);
  25. tabHost.addTab(spec);
  26. tabHost.setCurrentTab(2);
  27. }

7. 运行程序即可看到效果。

总结:

main.xml中定义的选项卡的样式,它和TabActivity共同配合,创建了选项卡的框架。TabHost.TabSpec 代表了一个选项卡的内容,TabHost使用addTab方法将其加入到整个选项卡中。

TabHost.TabSpec

原文: http://blog.csdn.net/cool_android/article/details/7202381

Android中选项卡功能的实现的更多相关文章

  1. Android中常见功能包描述(转)

    在Android中,各种包写成android.*的方式,重要包的描述如下所示:android.app :提供高层的程序模型.提供基本的运行环境android.content:包含各种的对设备上的数据进 ...

  2. Android中常见功能包描述

    在Android中,各种包写成android.*的方式,重要包的描述如下所示:android.app :提供高层的程序模型.提供基本的运行环境android.content:包含各种的对设备上的数据进 ...

  3. 【Android 应用开发】Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

  4. Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

  5. Android中Adapter总结

    根据一个制作列表的程序开始练手,结果就出现了学习安卓的第一个代码问题 运行程序发现,虽然报错,但是可以成功运行程序. Android中Adapter功能为 显示ListView,最常用的有ArrayA ...

  6. Android中自定义ListView实现上拉加载更多和下拉刷新

    ListView是Android中一个功能强大而且很常用的控件,在很多App中都有ListView的下拉刷新数据和上拉加载更多这个功能.这里我就简单记录一下实现过程. 实现这个功能的方法不止一个,Gi ...

  7. Android中使用GridView和ImageViewSwitcher实现电子相册简单功能

    我们在手机上查看相册时,首先看到的是网格状的图片展示界面,然后我们选择想要欣赏的照片点击进入,这样就可以全屏观看该照片,并且可以通过左右滑动来切换照片.如下图的显示效果: 首先我们先罗列一下本次实现所 ...

  8. Android中脱离WebView使用WebSocket实现群聊和推送功能

    WebSocket是Web2.0时代的新产物,用于弥补HTTP协议的某些不足,不过他们之间真实的关系是兄弟关系,都是对socket的进一步封装,其目前最直观的表现就是服务器推送和聊天功能.更多知识参考 ...

  9. Java乔晓松-android中调用系统拍照功能并显示拍照的图片

    android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...

随机推荐

  1. SpringMVC学习 -- IDEA 创建 HelloWorld

    SpringMVC 概述: Spring 为展现层提供的基于 MVC 实际理念的优秀 Web 框架 , 是目前最主流的 MVC 框架之一. 自 Spring3.0 发布及 2013 年 Struts ...

  2. [BZOJ3238][Ahoi2013]差异解题报告|后缀数组

    Description 先分析一下题目,我们显然可以直接算出sigma(len[Ti]+len[Tj])的值=(n-1)*n*(n+1)/2 接着就要去算这个字符串中所有后缀的两两最长公共前缀总和 首 ...

  3. bzoj 1433 二分图匹配

    裸地匈牙利或者最大流,直接匹配就行了 需要注意的是(我就没注意细节WA了好多次...) 每个人和自己之间的边是0,但是应该是1 不是在校生是没有床的.... /******************** ...

  4. Red-Black Tree

    A red-black tree is a Binary Search Tree that satisfy the red-black tree properties: 1. Every node i ...

  5. TensorFlow 官方文档中文版【转】

    转自:http://wiki.jikexueyuan.com/project/tensorflow-zh/ TensorFlow 官方文档中文版 你正在阅读的项目可能会比 Android 系统更加深远 ...

  6. Yeelight介绍

    1. 介绍 Yeelight是小米生态链中的WiFi智能灯泡,本文介绍它的接入和控制实现: Yeelight使用的是自定义的私有协议,该协议采用了类似SSDP的发现机制和基于JSON的控制命令 2. ...

  7. PHPstorm创建注释模版

    /** * $NAME$ * @param * @return * @since $DATE$ * @author Name */$END$ /** * xxxx -­- Controller – 类 ...

  8. 浅谈C#多线程与UI响应

    www.educity.cn     发布者:shenywww                    来源:网络转载                     发布日期:2014年10月06日      ...

  9. Centos7源码编译安装tengine1.5.1

    安装依赖包 yum install pcre pcre-devel openssl openssl-devel gcc make zlib-devel wget -y 下载和创建用户 mkdir /t ...

  10. Java-事务管理

    1.事务的概念: 事务指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功. 2. 管理事务: 2.1. 数据库默认的事务 数据库默认支持事务的,但是数据库默认的事务是一条sql语 ...