Tabhost以及其用法

TabHost是一种非常使用的组件,TabHost可以方便的在窗口上放置多个标签页,每个标签页相当于或得了一个与外部容器相同大小的组件摆放区域。

与TabHost 结合使用的组件

TabWidget:代表选项卡的标签条。

TabSpec:代表选项卡的一个Tab页面。

创建添加选项卡的方法:

newTabSpec():创建选项卡

addTab():添加选项卡

使用TabHost的步骤:

  1. 在界面布局中定义TabHost组件
  2. Activity继承TabActivity
  3. 调用TabActivity的getTabHost()方法获取TabHost
  4. 通过TabHost对象的方法来创建添加选项卡。
  5. <?xml version="1.0" encoding="utf-8"?>
  6. <LinearLayout
  7.     xmlns:android="http://schemas.android.com/apk/res/android"
  8.     android:layout_width="match_parent" android:layout_height="match_parent"
  9.    android:orientation="vertical"
  10.     >
  11.    
  12. <TabHost
  13.     android:layout_width="match_parent" android:layout_height="match_parent"
  14.     android:id="@android:id/tabhost"
  15.     android:layout_weight="1"
  16.     ><!--引用android系统已有的id-->
  17.     <LinearLayout
  18.         android:layout_width="match_parent"
  19.         android:layout_height="match_parent"
  20.         android:orientation="vertical"
  21.         >
  22.         <TabWidget
  23.             android:id="@android:id/tabs"
  24.             android:layout_width="match_parent"
  25.             android:layout_height="wrap_content"></TabWidget>//代表选项卡的标签条。
  26.         <FrameLayout
  27.             android:id="@android:id/tabcontent"
  28.             android:layout_width="match_parent"
  29.             android:layout_height="match_parent">
  30.             <!--定义第一个标签页的内容-->
  31.             <LinearLayout
  32.                 android:id="@+id/tab01"
  33.                 android:orientation="vertical"
  34.                 android:layout_width="fill_parent"
  35.                 android:layout_height="fill_parent">
  36.                 <TextView
  37.                     android:layout_width="fill_parent"
  38.                     android:layout_height="fill_parent"
  39.                     android:text="消息"
  40.                     android:textSize="30sp"
  41.                     />
  42.             </LinearLayout>
  43.             <LinearLayout
  44.                 android:id="@+id/tab02"
  45.                 android:orientation="vertical"
  46.                 android:layout_width="fill_parent"
  47.                 android:layout_height="fill_parent">
  48.                 <TextView
  49.                     android:layout_width="fill_parent"
  50.                     android:layout_height="fill_parent"
  51.                     android:text="赛事"
  52.                     android:textSize="30sp"
  53.                     />
  54.             </LinearLayout>
  55.             <LinearLayout
  56.                 android:id="@+id/tab03"
  57.                 android:orientation="vertical"
  58.                 android:layout_width="fill_parent"
  59.                 android:layout_height="fill_parent">
  60.                 <TextView
  61.                     android:layout_width="fill_parent"
  62.                     android:layout_height="fill_parent"
  63.                     android:text="我的页面"
  64.                     android:textSize="30sp"
  65.                     />
  66.             </LinearLayout>
  67.         </FrameLayout>
  68.  
  69.     </LinearLayout>
  70. </TabHost>
  71. </LinearLayout>
  72. 注意:必须使用这样的,其他的id是错误的
  73. android:id="@android:id/tabhost"
  74.  android:id="@android:id/tabs"
  75.  android:id="@android:id/tabcontent"
  76.  引用android系统已有的id
  77. public class MainActivity extends TabActivity{ //Activity继承TabActivity
  78.  
  79.     @Override
  80.     protected void onCreate(Bundle savedInstanceState) {
  81.         super.onCreate(savedInstanceState);
  82.         setContentView(R.layout.main_activity);
  83.          //调用TabActivity的getTabHost()方法获取TabHost
  84.         TabHost tabHost=getTabHost();
  85.         //创建第一个Tab页
  86.         TabHost.TabSpec tab1=tabHost.newTabSpec("tab1")

         

  87.                 .setIndicator("赛事")//设置标题
  88.                 .setContent(R.id.tab01);
  89.         //添加第一个tab页
  90.         tabHost.addTab(tab1);
  91.         TabHost.TabSpec tab2=tabHost.newTabSpec("tab2")
  92.                 .setIndicator("消息")//设置标题
  93.                 .setContent(R.id.tab02);
  94.         //添加第一个tab页
  95.         tabHost.addTab(tab2);
  96.  
  97.         TabHost.TabSpec tab3=tabHost.newTabSpec("tab3")
  98.                 .setIndicator("我")//设置标题
  99.                 .setContent(R.id.tab03);
  100.         //添加第一个tab页
  101.         tabHost.addTab(tab3);
  102.     }
  103. }
时间: 2024-08-01 15:11:04

Tabhost以及其用法的相关文章

修炼-------------Android TabHost,TabWidget选项卡总结

修炼-------------Android TabHost,TabWidget选项卡总结 Android之TabHost TabHost,个人理解为选项卡的容器,是一种特殊的FrameLayout布局(帧布局) 根据SDK文档, Container for a tabbed window view. This object holds two children: a set of tab labels that the user clicks to select a specific tab,

Android TabHost(简易用法)

前言 欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net 雨季o莫忧离:http://blog.csdn.net/luckkof 正文 Tab应用的结构 TabHost的Activity的结构如下: <?xml version="1.0" encoding="utf-8"?> <!-- 定义TabHost组件 --> <LinearLayout

TabHost的用法(转)

本文结合源代码和实例来说明TabHost的用法.      使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用,截图:       查看tabhost的源代码,主要实例变量有: private TabWidget mTabWidget;    private FrameLayout mTabContent;    private List<TabSpec> mTabSpecs 也就是说我们的tabhost必须有这三个东西,所以我们的.xml文件就会有规定:

TabHost的用法

今天学习了TabHost组件的功能和用法,在这里将Demos展现一下. activity_tab_host_demos.xml <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android

TabHost用法

tabhost用两种方法 方法一:Activity继承TabActivity后用getTabHost()方法来获取tabhost(前提:Activity的setContentView要删除,这样布局才能用TabActivity的默认布局,这样TabActivity的setup方法才不会出错),获取完后再对tabhost布局进行设置 方法二:用findViewById方法来获取tabhost,只是findViewById里的布局xml要符合一以下的格式 <?xml version="1.0&

Fragment + TabHost + RadioGroup

1. 使用FragMent是因为 4.0.3之后 ,摒弃了TabActivity这种用法, Demo 效果图: 先上布局XML R.layout.activity_main [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"

tabhost使用

Tabhost用法 使用方法一:使用同一个布局文件 在xml中如此定义tabhost: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heigh

Android零基础入门第70节:ViewPager轻松完成TabHost效果

上一期学习了ViewPager的简单使用,本期一起来学习ViewPager的更多用法. 相信很多同学都使用过今日头条APP吧,一打开主界面就可以看到顶部有很多Tab,然后通过左右滑动来切换,就可以通过ViewPager来完成.当然具体实现又会有很多方式,我们本期就先来学习最简单的Tab切换吧,有一点类似于之前了解的TabHost. 一.PagerTitleStrip与PagerTabStrip 在实际运用中,很多时候只有页面滑动是不够的,还需要有标题栏才够友好.首先来学习一下官方自带的,在and

实现360手机助手TabHost的波纹效果

现在新版360手机助手的界面都做得挺漂亮的,在切换底部导航时的波纹效果也很好看,刚好最近看了个开源项目才了解到原来Drawable做动画效果也怎么好用,所以就仿照360实现了下带波纹的TabHost.源代码地址:https://github.com/Rukey7/XFragmentTabHost 先来看一下实现后的效果: 说明一下实现要点: 1. 因为我们项目之前用的是FragmentTabHost,所以我直接继承FragmentTabHost来实现动画效果更方便: 2. 波纹动画的实现其实是自