本例介绍TabActivity ,在Android新版本不建议使用TabActivity,而是使用Fragment。 TabActivity 提供了分页控件(Tab Control),关键的一个类为TabHost,TabHost 为TabActivity的View,可以通过getTabHost()取得TabActivity 对应的TabHost.

TabHost 可以通过TabHost.TabSpec 来为Tab 窗口添加一个页面,TabSpec 用来描述一个页面(Tab) ,每个Tab 可以有个tag 用来区分Tab窗口的某个页面,indicator (页标题,可以使用文字,图标等来显示) 和Content(页面内容)。

其中Content 使用View资源Id或是TabHost.TabContentFactory构造或是某个Intent实例(该Intent可以启动某个Activity)。这也是为什么TabActivity 为一ActivityGroup 的原因。

本例对应的代码为Tab1.java 采用View资源ID的方法来设置Tab的Content:

TabHost tabHost = getTabHost();LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1") .setContent(R.id.view1));tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab2") .setContent(R.id.view2));tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3") .setContent(R.id.view3));

要注意的是使用setContent中指定的View,之前需要将它们展开到tabHost.getTabContentView()中。

R.layout.tabs1.xml 定义如下:

<FrameLayout xmlns:android=”http://schemas.android.com/apk/res/android”android:layout_width=”match_parent”android:layout_height=”match_parent”><TextView android:id=”@+id/view1″android:background=”@drawable/blue”android:layout_width=”match_parent”android:layout_height=”match_parent”android:text=”@string/tabs_1_tab_1″/><TextView android:id=”@+id/view2″android:background=”@drawable/red”android:layout_width=”match_parent”android:layout_height=”match_parent”android:text=”@string/tabs_1_tab_2″/><TextView android:id=”@+id/view3″android:background=”@drawable/green”android:layout_width=”match_parent”android:layout_height=”match_parent”android:text=”@string/tabs_1_tab_3″/></FrameLayout>

Android ApiDemos示例解析(193):Views->Tabs->Content By Id_第1张图片


更多相关文章

  1. Android资源文件xml的编写
  2. Android简单类型资源(Bool、Color、Dimension、ID、Integer、Inte
  3. Android 结合WindowManager和WindowManager.LayoutParams显示窗
  4. 第一个Android登录页面 | HTTP 请求验证登录 | 完整代码
  5. android资源网址大全
  6. Android Studio学习:中级控件:使用Intent进行页面之间数据传输
  7. Android中的资源文件
  8. android页面切换动画

随机推荐

  1. [Android]Smali语法
  2. Android 翻牌动画 实现
  3. Android中判断网络是否连接实例详解
  4. FregServer进程,启动Binder线程池,睡眠等待
  5. android与服务器连接保持
  6. 轻松实现Android锁屏功能
  7. Android《内嵌浏览器-WebView》
  8. Android进程保活招式大全
  9. Mac无法编译android
  10. 31、android打开文件夹并列出里面的文件