Android之TabHost

简介:

一. 简单示例

src

[java]  view plain copy
  1. public class AndroidUIActivity extends TabActivity {  
  2.   
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.   
  8.         // tabHost是一个标签容器  
  9.         TabHost tabHost = this.getTabHost();  
  10.   
  11.         // 每一个TabSpec对象就是个标签  
  12.         // TabSpec.setIndicator()方法是设置标签显示样式  
  13.         // TabSpec.setContent()方法显示标签下方的内容显示  
  14.   
  15.         // 定义第一个标签  
  16.         tabHost.addTab(tabHost  
  17.                 .newTabSpec("OneTab")  
  18.                 .setIndicator("OneTab",  
  19.                         getResources().getDrawable(android.R.drawable.star_on))  
  20.                 .setContent(R.id.linearLayout1));  
  21.   
  22.         // 定义第二个标签  
  23.         tabHost.addTab(tabHost  
  24.                 .newTabSpec("TwoTab")  
  25.                 .setIndicator("TwoTab",  
  26.                         getResources().getDrawable(android.R.drawable.star_off))  
  27.                 .setContent(R.id.linearLayout2));  
  28.   
  29.         // 定义第三个标签  
  30.         tabHost.addTab(tabHost  
  31.                 .newTabSpec("ThreeTab")  
  32.                 .setIndicator("ThreeTab",  
  33.                         getResources().getDrawable(android.R.drawable.stat_notify_call_mute))  
  34.                 .setContent(R.id.linearLayout3));  
  35.   
  36.     }  
  37. }  

main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3.      根元素是 TabHost ,我们这个文件要和TabActivity配合使用,在TabActivity的源代码里写死了要找的Id是android.R.id.tabhost,  
  4.     因此这里的ID也要定死成TabHost 的ID 是定死的 "@android:id/tabhost" 下面的类似,不再解释。  
  5.   
  6. -->  
  7. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  8.     android:id="@android:id/tabhost"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content" >  
  11.   
  12.     <!-- TabWidget 就是标签选项卡的头部部分,注意ID的写法 -->  
  13.   
  14.     <TabWidget  
  15.         android:id="@android:id/tabs"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content" >  
  18.     </TabWidget>  
  19.     <!-- FrameLayout 就是标签的内容显示部分,注意ID的写法,还要注意我们做了个上部空白设定 android:paddingTop="65dp",是为了不让内容和标签重叠 -->  
  20.   
  21.     <FrameLayout  
  22.         android:id="@android:id/tabcontent"  
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="fill_parent" >  
  25.   
  26.         <!-- LinearLayout 是一个标签里的内容,程序根据你定义的ID来把他们放在不同的标签下面     android:paddingtop="65dp" -->  
  27.   
  28.         <LinearLayout  
  29.             android:id="@+id/linearLayout1"  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content" >  
  32.   
  33.             <TextView  
  34.                 android:id="@+id/textView1"  
  35.                 android:layout_width="wrap_content"  
  36.                 android:layout_height="wrap_content"  
  37.                 android:text="标签1中的内容" >  
  38.             </TextView>  
  39.         </LinearLayout>  
  40.         <!-- LinearLayout 是另一个标签里的内容 -->  
  41.   
  42.         <LinearLayout  
  43.             android:id="@+id/linearLayout2"  
  44.             android:layout_width="wrap_content"  
  45.             android:layout_height="wrap_content" >  
  46.   
  47.             <TextView  
  48.                 android:id="@+id/textView2"  
  49.                 android:layout_width="wrap_content"  
  50.                 android:layout_height="wrap_content"  
  51.                 android:text="标签2中的内容" >  
  52.             </TextView>  
  53.         </LinearLayout>  
  54.   
  55.         <LinearLayout  
  56.             android:id="@+id/linearLayout3"  
  57.             android:layout_width="wrap_content"  
  58.             android:layout_height="wrap_content" >  
  59.   
  60.             <TextView  
  61.                 android:id="@+id/textView3"  
  62.                 android:layout_width="wrap_content"  
  63.                 android:layout_height="wrap_content"  
  64.                 android:text="标签3中的内容" >  
  65.             </TextView>  
  66.         </LinearLayout>  
  67.     </FrameLayout>  
  68.   
  69. </TabHost>  

二. 运行结果

启动



点击ThreeTab

相关文章
|
XML Android开发 数据格式
【Android 应用开发】Android - TabHost 选项卡功能用法详解
【Android 应用开发】Android - TabHost 选项卡功能用法详解
403 0
【Android 应用开发】Android - TabHost 选项卡功能用法详解
|
Android开发 数据格式 XML
|
XML Android开发 数据格式
Android零基础入门第63节:过时但仍值得学习的选项卡TabHost
原文:Android零基础入门第63节:过时但仍值得学习的选项卡TabHost     由于前几天参加一个学习培训活动,几乎每天都要从早晨7点到晚上一两点,没有什么时间来分享,实在抱歉中间断更了几天。从今天开始恢复分享,更多精彩敬请期待。
1472 0

热门文章

最新文章

  • 1
    Android实战经验之Kotlin中快速实现MVI架构
    344
  • 2
    即时通讯安全篇(一):正确地理解和使用Android端加密算法
    210
  • 3
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
    553
  • 4
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    854
  • 5
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    313
  • 6
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    230
  • 7
    Android历史版本与APK文件结构
    739
  • 8
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    245
  • 9
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    259
  • 10
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
    491