(一)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. MFC_1.2 消息映射宏 数据绑定和交换

    消息映射宏 有三个主要的宏 类内声明 DECLARE_MESSAGE_MAP 表示使用消息映射 在CPP文件中使用 BEGIN_MESSAGE_MAP 和 END_MESSAGE_MAP 包含对应的消 ...

  2. Vue的特性

    1.数据驱动视图 <div id="app"> <p> {{ message }}<p> </div> var app = new ...

  3. RabbitMQ系列(七)--批量消息和延时消息

    批量消息发送模式 批量消息是指把消息放到一个集合统一进行提交,这种方案设计思路是希望消息在一个会话里,比如放到threadlocal里的集合,拥有相同 的会话ID,带有这次提交信息的size等属性,最 ...

  4. 我的ACM技能框架(自用)

    每次接触到新的知识就把它名字记下来,留给以后当纪念 2018.11 已经学会的 滚动数组,前缀和优化 对多维数组在空间复杂度上的降维优化     最长上升子序列 LIS问题,动态规划递推解决 最长不下 ...

  5. Go:值类型、引用类型

    值类型,变量存的就是值本身: in系列t.float系列.bool.string.数组和struct 引用类型,变量存的是一个地址,这是地址存的才是值本身: 指针.slice.map.chan.int ...

  6. vue v-model 的注意点

    在使用表单元素 input 的 v-model 指令时,碰到一个问题: 如上图,修改 input 的内容,以便随时显示:但显示时明显慢一步. <template> <div> ...

  7. DAS、NAS、SAN、iSCSI 存储方案概述

    (转自https://blog.csdn.net/tianlesoftware/article/details/6201346) 目前服务器所使用的专业存储方案有DAS.NAS.SAN.iSCSI几种 ...

  8. 回车符号 ‘\r’ 的实际应用

    由于最近开始研究自动化测试 首先是自动定时去下载安装包,需要实时显示进度. 于是了解了进度条相关的方法. 作下记录. 区别 \r 表示将光标的位置回退到本行的开头位置 \n 表示光标从下一行的开头位置 ...

  9. Greek symbols --Latex

    $\propto$     \propto $\infty$   \infty $\ne$   \ne $\approx$     \approx $\sim$ :    \sim  --- same ...

  10. BZOJ 3916 [Baltic2014]friends

    [题解] 暴力枚举添加字符的位置,然后判断去掉这个位置的字符串是否有由两个相同的串拼接而成.本题的n的范围为200W,暴力判断会TLE,所以要使用哈希. #include<cstdio> ...