(一)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. (转)全文检索技术学习(三)——Lucene支持中文分词

    http://blog.csdn.net/yerenyuan_pku/article/details/72591778 分析器(Analyzer)的执行过程 如下图是语汇单元的生成过程:  从一个Re ...

  2. 计算机网络(二)--HTTP详解

    Web相关内容都是存储在Web服务器上,Web服务器上使用的是http协议,因此也被成为http服务器.http的client.server构成了万维网的 基本组件 一.资源 1.URI: 统一资源标 ...

  3. 04Microsoft SQL Server 数据库创建,查看,使用,修改及删除

    Microsoft SQL Server 数据库创建,查看,使用,修改及删除 创建数据库 创建普通数据库 USE [master] GO CREATE DATABASE [MyDataBase] -- ...

  4. Windows下运行jekyll,编码已不再是问题

    很久没更新jekyll了,所以好奇着去官网看了下更新记录,发现如下更新条目(版本1.3.0/2013-11-04发布): Add encoding configuration option (#144 ...

  5. UVA - 1615 Highway(贪心-区间选点问题)

    题目: 给定平面上n(n≤105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D. 思路: 先自己造区间,然后贪心选点就可以了.之前做过一 ...

  6. Codeforces Round #530 (Div. 2) (前三题题解)

    总评 今天是个上分的好日子,可惜12:30修仙场并没有打... A. Snowball(小模拟) 我上来还以为直接能O(1)算出来没想到还能小于等于0的时候变成0,那么只能小模拟了.从最高的地方进行高 ...

  7. python求两个链表组成的数字的和

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  8. buf.writeUIntBE()函数详解

    buf.writeUIntBE(value, offset, byteLength[, noAssert]) buf.writeUIntLE(value, offset, byteLength[, n ...

  9. java mysql prepareStatement模糊查询like使用注意

    今天在使用mysql 的like语句是,发现prepareStatement的like语句和一般的=写法有一样. 当要使用prepareStatement的like查询时,按照一般写法,都会写成: S ...

  10. ubutun 创建左面快捷方式

    #http://blog.csdn.net/jizi7618937/article/details/51012552