1.布局XML:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:id="@android:id/tabhost">
  6. <RelativeLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:orientation="vertical">
  10. <TextView
  11. android:id="@+id/bottomtip"
  12. android:layout_width="match_parent"
  13. android:layout_height="50dp"
  14. android:text="@string/funPageBtn2"
  15. android:gravity="center_horizontal|center_vertical"
  16. android:layout_alignParentTop="true"
  17. android:textColor="@android:color/white"
  18. android:background="@android:color/black"
  19. />
  20. <FrameLayout
  21. android:id="@android:id/tabcontent"
  22. android:layout_width="match_parent"
  23. android:layout_height="match_parent"
  24. android:layout_above="@android:id/tabs"
  25. android:layout_below="@+id/bottomtip"
  26. />
  27. <TabWidget
  28. android:id="@android:id/tabs"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:layout_alignParentBottom="true"
  32. android:background="@android:color/black"
  33. />
  34. </RelativeLayout>
  35.  
  36. </TabHost>

布局效果理解:

2.Activity类实现

  1. package com.test.appdemo1.actlearn;
  2.  
  3. import com.test.appdemo1.R;
  4.  
  5. import android.app.TabActivity;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.widget.TabHost;
  9. import android.widget.TabHost.TabSpec;
  10.  
  11. @SuppressWarnings("deprecation")
  12. public class TabOptTopAct extends TabActivity {
  13.  
  14. private TabHost tabHost;
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.tabtoplayout);
  18. initTab();
  19. }
  20.  
  21. private void initTab() {
  22. tabHost = getTabHost();
  23. TabSpec tabSpec = null;
  24. Intent intent = new Intent(this,MainActivity.class);
  25. //第一个
  26. tabSpec = tabHost.newTabSpec("tab1")
  27. .setIndicator("首页",getResources().getDrawable(R.drawable.home))
  28. .setContent(intent);
  29. tabHost.addTab(tabSpec);
  30.  
  31. //第二个
  32. intent = new Intent(this,NextActivity.class);
  33. tabSpec = tabHost.newTabSpec("tab2")
  34. .setIndicator("邮件",getResources().getDrawable(R.drawable.mail))
  35. .setContent(intent);
  36. tabHost.addTab(tabSpec);
  37.  
  38. //第三个
  39. intent = new Intent(this,ServiceActDemo.class);
  40. tabSpec = tabHost.newTabSpec("tab3")
  41. .setIndicator("音乐",getResources().getDrawable(R.drawable.music))
  42. .setContent(intent);
  43. tabHost.addTab(tabSpec);
  44.  
  45. //第四个
  46. intent = new Intent(this,FrameLearnLayoutAct.class);
  47. tabSpec = tabHost.newTabSpec("tab4")
  48. .setIndicator("计算",getResources().getDrawable(R.drawable.calc))
  49. .setContent(intent);
  50. tabHost.addTab(tabSpec);
  51.  
  52. tabHost.setCurrentTab(1);
  53. }
  54.  
  55. public void onBackPressed() {
  56. super.onBackPressed();
  57. finish();
  58. }
  59. }

最后说明:如果想把选项卡放到顶部只需修改布局文件中RelativeLayout里面的三个元素的位置

若有问题请大家指正.

程序运行效果如上图片所示.

Android选项卡TabHost方式实现的更多相关文章

  1. android 选项卡TabHost

    选项卡主要有TabHost.TabWiget和 FramentLayout3个组件组成,用于实现一个多标签的用户界面,通过他可以将一个复杂的对话分隔成若干个标签页,实现对信息的分类显示和管理.使用给组 ...

  2. Android选项卡TabHost功能和用法

    1.布局文件 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  3. Android 自学之选项卡TabHost

    选项卡(TabHost)是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组建摆放区域.通过这种方式,就可以在一个容器中放置更多组件 ...

  4. Android零基础入门第63节:过时但仍值得学习的选项卡TabHost

    原文:Android零基础入门第63节:过时但仍值得学习的选项卡TabHost 由于前几天参加一个学习培训活动,几乎每天都要从早晨7点到晚上一两点,没有什么时间来分享,实在抱歉中间断更了几天.从今天开 ...

  5. android学习--TabHost选项卡组件

    TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...

  6. Android 通过findViewById方式创建TabHost

    package org.shuxiang.tabhostsample; import android.os.Bundle; import android.app.ActivityGroup; impo ...

  7. 12.Android之Tabhost组件学习

    TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...

  8. Android:TabHost实现Tab切换

    TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容. 实现方式有两种: 1.继承TabA ...

  9. Android学习Tabhost、gallery、listview、imageswitcher

    Tabhost控件又称分页控件,在很多的开发语言中都存在.它可以拥有多个标签页,每个标签页可以拥有不同的内容.android中,一个标签页可以放 一个view或者一个activity.TabHost是 ...

随机推荐

  1. flash链接需要后台调用时的插入flash方法

    <!--<textarea id="syslink" style="display:none;">// 1:打宝塔 2:山寨玩法 3:跨服竞技 ...

  2. easyui 使特定tab处于选中状态

    <div id="tabs" class="easyui-tabs"> <div title="Tab1" style=& ...

  3. POJ 1850 Code(组合数)

    http://poj.org/problem?id=1850 题意 :给定字符串,系统是用字符串组成的,字符串是按字典序排的.编码系统有三条规则,1这些的单词的长度是由小到大的,2相同长度的按字母在字 ...

  4. poj 2265 Bee Maja

    题目的意思很容易理解.就是找两个不同坐标的对应关系.下面的思路转自POJ的论坛 首先,记由1到2的方向记为2,1到3的方向记为3……1到7的方向记为7,他们分别是:(0,1),(-1,1),(-1,0 ...

  5. FileZilla Server 防火墙端口开启设置 windows 2008 win

    入站规则 添加21端口, 程序FileZilla server.exe 出站规则 %SystemRoot%\System32\ftp.exe

  6. NSString的常用方法

    创建一个新字符串并将其设置为 path 指定的文件的内容,使用字符编码enc,在error上返回错误 + (id)stringWithContentsOfURL:(NSURL *)url encodi ...

  7. 123. Best Time to Buy and Sell Stock III

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  8. 编程之美:1.9高效率安排见面会 图的m着色问题 回溯法

    原书问题,可以转换为图的m着色问题 ,下面该问题的代码 这里有参考ppt与code,免积分载 http://download.csdn.net/detail/u011467621/6341195 // ...

  9. java socket nio编程

    上次写了一个socket的基本编程,但是有个问题,阻塞特别严重,于是小编便去找了nio学习了一下... public class TimeServer { public static void mai ...

  10. HTTPConnection与HTTPClient的区别

    HttpClient是个很不错的开源框架,封装了访问http的请求头,参数,内容体,响应等等.HttpURLConnection是java的标准类,什么都没封装,用起来太原始,不方便.比如重访问的自定 ...