(一)TabHost控件,默认是在顶部显示的

TabHost是盛放Tab按钮和Tab内容的首要容器,

TabWidget(tabs标签)用于选择页面,是指一组包含文本或图标的 ,FrameLayout 用于显示页面的内容,是构成Tab页的容器。

注意: (使用系统自带的id,格式为@android:id/)

TabHost (@android:id/tabhost)

FrameLayout(@android:id/tabcontent),

TabWidget( @android:id/tabs)

(二)TabHost的两种跳转方式

一种是利用Layout:

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(Tab1,getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab1));

一种是利用Intent:

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(Tab1,getResources().getDrawable(R.drawable.icon)).setContent(new Intent(this,OtnerActivity.class)));

Tabhost的activity文件分为两种情况,一种是继承自Activity,一种是继承者TabActivity,二者的不同之处在于tabhost的初始化方式不同,跳转的方式相同。

继承Activity :

setContentView(R.layout.***); //设置上面的xml文件

TabHost tabhost = (TabHost) findViewById(R.id.tabhost);

tabhost.setup();   // 初始化TabHost容器

注意加.setup(),否则会有NullPointer的异常

继承TabActivity 类,

TabHost tabHost = getTabHost();//获取当前TabHost对象

LayoutInflater.from(this).inflate(R.layout.main,tabHost.getTabContentView(), true);  //设置使用tabhost布局

添加Tab分页标签(添加选项卡及设置选项的标题及内容 我们知道添加选项卡需要指定一个TabSpec对象,通过TabHost的newTabSpec(选项卡的标识)可以得到,)

tabHost.addTab(tabHost.newTabSpec("tab1")   .setIndicator("tab1", getResources().getDrawable(R.drawable.a1))  .setContent(R.id.view1));

所谓TabSpec就是在Tabwidget中显示的那一个个的小格子,addTab(TabSpec)就是增加一个小格子。

TabSpec主要的方法就是setContent()和setIndicator(),设置的参数不同,设置的内容不同,(详解见上面跳转方式的两个例子)

现象:  在Tabhsot中使用intent去打开一个界面是给  Tabhsot.Tabspec页通过setcontent(intent)方法实现跳转

用intent携带数据只能使用一次

若使用多次,  跳转到得页面都只能拿到第一次设置的数据内容。

原因:在Tabhsot.Tabspec的setcontent方法中将intent给final了。

(三)一个典型的TabHost的例子:

XML文件:

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/tabhost"     android:layout_width="match_parent"

android:layout_height="match_parent" >

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TabWidget

android:id="@android:id/tabs"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"  />

<FrameLayout

android:id="@android:id/tabcontent"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<AnalogClock

android:id="@+id/AnalogClock03"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#000000" />

<DigitalClock

android:id="@+id/DigitalClock01"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_centerHorizontal="true"

android:background="#000000" />

</FrameLayout>

</LinearLayout>

</TabHost>

Java代码:

public class TabDemoActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.setTitle("演示标签分页〉");

//获取TabHost对象

TabHost tabHost=(TabHost) this.findViewById(R.id.tabhost);

tabHost.setup();

 //新建一个newTabSpec,设置标签和图标(setIndicator),设置内容(setContent)

TabHost.TabSpec spec=tabHost.newTabSpec("clockTab");

spec.setContent(R.id.AnalogClock03);

spec.setIndicator("模拟时钟", getResources().getDrawable(android.R.drawable.ic_btn_speak_now));

tabHost.addTab(spec);

spec=tabHost.newTabSpec("buttonTab");

spec.setContent(R.id.DigitalClock01);

spec.setIndicator("数字时钟",getResources().getDrawable(android.R.drawable.btn_star_big_on));

tabHost.addTab(spec);

    // 设置TabHost的背景颜色

   tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));

// 设置TabHost的背景图片资源

// tabHost.setBackgroundResource(R.drawable.bg);

// 设置当前现实哪一个标签    tabHost.setCurrentTab(0);

   // 0为标签ID

// 标签切换处理,用setOnTabChangedListener

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

   public void onTabChanged(String tabId) {

      Toast.makeText(TabDemoActivity.this, "This is a Test!",

      Toast.LENGTH_LONG).show();

     }

   });

} }

(四)android实现底部TabHost

在TabWidget控件中,通过设置android:layout_alignParentBottom="true"属性实现底部TabHost

(五)TabHost改进

如果不希望默认加载事选中一项,而是做成新浪微博底部控制栏的风格,则需要给TabWidget控件添加属性android:visibility="gone",让后给TabWidget添加若干RadioButton子控件,然后将RadioButton设置成自己想要的样式即可。

XML文件如下:
<?xml version="1.0" encoding="UTF-8"?><TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"   xmlns:android="http://schemas.android.com/apk/res/android">     <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">         <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" />         <TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" />         <RadioGroup android:gravity="center_vertical" android:layout_gravity="bottom" android:orientation="horizontal" android:id="@id/main_radio" android:background="@drawable/maintab_toolbar_bg" android:layout_width="fill_parent" android:layout_height="wrap_content">             <RadioButton   android:text="@string/main_home" android:checked="true" android:id="@+id/radio_button0" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_1_n" style="@style/main_tab_bottom" />             <RadioButton android:id="@+id/radio_button1" android:layout_marginTop="2.0dip" android:text="@string/main_news" android:drawableTop="@drawable/icon_2_n" style="@style/main_tab_bottom" />             <RadioButton android:id="@+id/radio_button2" android:layout_marginTop="2.0dip" android:text="@string/main_my_info" android:drawableTop="@drawable/icon_3_n" style="@style/main_tab_bottom" />             <RadioButton android:id="@+id/radio_button3" android:layout_marginTop="2.0dip" android:text="@string/menu_search" android:drawableTop="@drawable/icon_4_n" style="@style/main_tab_bottom" />             <RadioButton android:id="@+id/radio_button4" android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" style="@style/main_tab_bottom" />         </RadioGroup>     </LinearLayout></TabHost>

参考网址:

http://blog.sina.com.cn/s/blog_8373f9b501018b45.html

http://www.cnblogs.com/over140/archive/2011/03/02/1968042.html

android TabHost控件的更多相关文章

  1. Android TabHost控件 右侧留空并增加按钮

    涉及公司内部程序,部分地方进行模糊处理. 公司Android程序的一个子程序UI要进行改版,最初的UI添加按钮是在内容区,而且TabHost空间是正常的标题平均分布.如下图(其实这是改版的第一版,没有 ...

  2. 一个Demo让你掌握Android所有控件

    原文:一个Demo让你掌握Android所有控件 本文是转载收藏,侵删,出处:"安卓巴士"      下面给出实现各个组件的源代码: 1.下拉框实现--Spinner packag ...

  3. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  4. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  5. Android基本控件之Menus

    在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...

  6. Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...

  7. Android:控件布局(线性布局)LinearLayout

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

  8. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

  9. Android给控件添加触摸回调

    Android给控件添加触摸回调 脑补一个场景,一个页面点击某个按钮会弹出PopupWindow,然后点击PopupWindow以外的任意位置关闭 效果图 实现方法 可以在布局的最外层容器监听触摸事件 ...

随机推荐

  1. php利用array_filter()过滤数组空值

    利用array_filter过滤数组空值 <?php $array = array( 0 => '霜天部落', 1 => false, 2 => 1, 3 => null ...

  2. vue基础---介绍

    (1)声明式渲染 Vue.js 的核心是采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统: ①文本 <div id="app"> {{ message }} & ...

  3. 02Microsoft SQL Server 安装,卸载,系统服务,系统组件及系统数据库

    Microsoft SQL Server 安装,卸载,系统服务,系统组件及系统数据库 1. Microsoft SQL Server 安装 通过单击下拉框,选择浏览,然后在Active Directo ...

  4. scala学习(3)-----wordcount【sparksession】

    参考: spark中文官方网址:http://spark.apachecn.org/#/ https://www.iteblog.com/archives/1674.html 一.知识点: 1.Dat ...

  5. Spring Boot 与任务

    一.任务 1.异步任务 package com.yunche.task.service; import org.springframework.stereotype.Service; /** * @C ...

  6. Android 项目Log日志输出优化

    概述 Android开发过程中经常需要向控制台输出日志信息,有些人还在用Log.i(tag,msg)的形式或者system.out.println(msg)方式吗?本篇文章对日志信息输出进行优化,以达 ...

  7. 关于fragment+viewpager的优化

    上次写了一个问答项目,用的fragment+viewpager架构,后来发现,划了几次之后,再划回来,会重新加载布局,重新获取数据,这样整个程序和卡,并且占用太多的网络资源. 当时的解决办法是,自己重 ...

  8. Re0:DP学习之路 数塔 HDU - 2084(基础递推)

    解法 首先是输入的问题,输入的时候还要注意每一层都有多少个 然后是怎么求解,一般求解首先要考虑顺序,是正序还是倒序 如果这个题是正序的话那么最终还需要将最后一行进行一次找max的运算 如果是倒序的话那 ...

  9. IN语句改写EXISTS

    -- IN SELECT T1.* FROM role_menu T1 WHERE T1.ROLEUUID IN ( SELECT T2.uuid FROM role T2 WHERE T2.UUID ...

  10. 在eclipse中如何在大量项目中查找指定文件

    在eclipse中如果希望在大量的项目中寻找指定的文件可不是一件轻松的事,还好eclipse提供了强大的搜索功能. 我们可以通过通配符或正则表达式来设定查寻条件,下面是操作示例: ctrl+h 打开搜 ...