转自:http://www.itivy.com/android/archive/2011/6/27/android-tabactivity-usage.html

如果希望在Activity中出现多个Tab可以点击,并且点击每个Tab之后跳转到相应的Activity,可以使用TabActivity类。以下演示一个简单的范例。

首先要定义一个继承TabActivity的类,这里我们定义MainActivity,并且使其作为应用程序的入口。其代码为

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 package com.myandroid.tabtest; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TabHost; import android.widget.TabHost.TabSpec; public class MainActivity extends TabActivity { private TabHost m_tabHost; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabs); //getTabHost返回的TabHost用于装载tabs m_tabHost = getTabHost(); //add tabs,这里用于添加具体的Tabs,并用Tab触发相应的Activity addOneTab(); addTwoTab(); addThreeTab(); addFourTab(); } public void addOneTab(){ Intent intent = new Intent(); intent.setClass(MainActivity. this , OneActivity. class ); TabSpec spec = m_tabHost.newTabSpec( "One" ); spec.setIndicator(getString(R. string .one), null ); spec.setContent(intent); m_tabHost.addTab(spec); } public void addTwoTab(){ Intent intent = new Intent(); intent.setClass(MainActivity. this , TwoActivity. class ); TabSpec spec = m_tabHost.newTabSpec( "Two" ); spec.setIndicator(getString(R. string .two), null ); spec.setContent(intent); m_tabHost.addTab(spec); } public void addThreeTab(){ Intent intent = new Intent(); intent.setClass(MainActivity. this , ThreeActivity. class ); TabSpec spec = m_tabHost.newTabSpec( "Three" ); spec.setIndicator(getString(R. string .three), null ); spec.setContent(intent); m_tabHost.addTab(spec); } public void addFourTab(){ Intent intent = new Intent(); intent.setClass(MainActivity. this , FourActivity. class ); TabSpec spec = m_tabHost.newTabSpec( "Four" ); spec.setIndicator(getString(R. string .four), null ); spec.setContent(intent); m_tabHost.addTab(spec); } }

可以看到在MainActivity中,我们使用getTabHost()返回一个TabHost,而TabHost正是用来添加Tabs的。这里 我们添加了4个Tabs,使用4个函数完成:addOneTab(),addTwoTab(),addThreeTab(),addFourTab(). 在这4个函数中我们使用TabSpec来描述每个的Tab,并且设置Intent,完成点击该Tab时跳转到相应的Activity的功能。

当然,这个应用还有一个关键点,就是这里的布局tabs.xml.其代码为:

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

注意在tabs.xml中,定义TabHost标签,并且其中有一个TabWidget标签是装载整个Tabs的,其id必须为android:id/tabs

完成这些工作之后,接下来的任务就是定义前面的几个Activity,OneActivity,TwoActivity,ThreeActivity,FourActivity.这些Activity由大家根据自己的功能设定。

更多相关文章

  1. Android手机拨打电话、手动发送短信与自动拨打电话、自动发送短
  2. Android SDK编译 ——如何将自己源代码加入android.jar
  3. Android 高德地图自定义定位图标的显示
  4. Android 高级混淆和代码保护技术
  5. android 自定义键盘 KeyboardView的key 文字颜色发虚模糊
  6. Android Audio代码分析1 - AudioTrack使用示例
  7. Android中自定义标题栏样式的两种方法
  8. Android 动态加载APK--代码安装、获取资源及Intent调用已安装apk
  9. Android中自定义下拉样式Spinner

随机推荐

  1. 一篇文章搞懂Handler发消息时,Handler,Mess
  2. Retrofit2源码分析(2) CallAdapter详解
  3. RelativeLayout相对布局介绍及属性介绍
  4. Android(安卓)录制桌面视频 screenrecord
  5. Android-传递数据的一些讨论
  6. 【Android(安卓)Developers Training】 3
  7. Android新浪微博客户端(五)——主界面的Tab
  8. Android(安卓)studio把一段代码快速提取,
  9. android四大组件&helloworld例子
  10. Android通过编译源代码提供系统服务-andr