(一)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

最新文章

  1. [LeetCode] LRU Cache 最近最少使用页面置换缓存器
  2. 关于php编程的一些小技巧
  3. 我的J2EE学习历程
  4. 实用命令dd
  5. html5菜单折纸效果
  6. debian7 编译qtopia错误解决案例
  7. mahout安装配置
  8. JDK Tools - jinfo: Java 配置信息工具
  9. Bootstrap_排版_表格
  10. 【译】UI设计基础(UI Design Basics)--为iOS设计(Design for iOS)(二)
  11. javascript基础学习(十四)
  12. [转]在 SQL Server 2008 中新建用户登录并指定该用户的数据库
  13. C++面试问题总结
  14. POJ 半平面交 模板题 三枚
  15. WEB安全测试通常要考虑的测试点
  16. css3 loading动画 三个省略号
  17. fireasy 使用篇 - 简介
  18. spring boot(二):启动原理解析
  19. leecode第一百五十五题(最小栈)
  20. 解决Eclipse中DDMS一直打印输出Connection attempts的问题

热门文章

  1. (转)Hibernate框架基础——映射集合属性
  2. (转)Hibernate框架基础——映射普通属性
  3. 【OptiX】第5个示例 递归反射、抗锯齿
  4. 网络编程基础_4.1TCP_服务端
  5. 个人作业Alpha项目测试
  6. 梦想CAD控件关于曲线问题
  7. java虚拟机(四)--内存溢出、内存泄漏、SOF
  8. 【Js 文件】 相关
  9. C++中重载,重写,隐藏的区别
  10. 51nod 1096 距离之和最小 1108 距离之和最小 V2