1.布局XML:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/bottomtip"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/funPageBtn2"
android:gravity="center_horizontal|center_vertical"
android:layout_alignParentTop="true"
android:textColor="@android:color/white"
android:background="@android:color/black"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@android:id/tabs"
android:layout_below="@+id/bottomtip"
/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/black"
/>
</RelativeLayout> </TabHost>

布局效果理解:

2.Activity类实现

package com.test.appdemo1.actlearn;

import com.test.appdemo1.R;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; @SuppressWarnings("deprecation")
public class TabOptTopAct extends TabActivity { private TabHost tabHost;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabtoplayout);
initTab();
} private void initTab() {
tabHost = getTabHost();
TabSpec tabSpec = null;
Intent intent = new Intent(this,MainActivity.class);
//第一个
tabSpec = tabHost.newTabSpec("tab1")
.setIndicator("首页",getResources().getDrawable(R.drawable.home))
.setContent(intent);
tabHost.addTab(tabSpec); //第二个
intent = new Intent(this,NextActivity.class);
tabSpec = tabHost.newTabSpec("tab2")
.setIndicator("邮件",getResources().getDrawable(R.drawable.mail))
.setContent(intent);
tabHost.addTab(tabSpec); //第三个
intent = new Intent(this,ServiceActDemo.class);
tabSpec = tabHost.newTabSpec("tab3")
.setIndicator("音乐",getResources().getDrawable(R.drawable.music))
.setContent(intent);
tabHost.addTab(tabSpec); //第四个
intent = new Intent(this,FrameLearnLayoutAct.class);
tabSpec = tabHost.newTabSpec("tab4")
.setIndicator("计算",getResources().getDrawable(R.drawable.calc))
.setContent(intent);
tabHost.addTab(tabSpec); tabHost.setCurrentTab(1);
} public void onBackPressed() {
super.onBackPressed();
finish();
}
}

最后说明:如果想把选项卡放到顶部只需修改布局文件中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. 839. Optimal Marks - SPOJ

    You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range ...

  2. 【数学】[BZOJ 3884] 上帝与集合的正确用法

    Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“元” ...

  3. Log4net Level

    ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); l ...

  4. python读写配置文件

    #coding:utf-8 import ConfigParser class Conf(): def __init__(self,name): self.name = name self.cp = ...

  5. C++11多线程教学(二)

    C++11多线程教学II 从我最近发布的C++11线程教学文章里,我们已经知道C++11线程写法与POSIX的pthreads写法相比,更为简洁.只需很少几个简单概念,我们就能搭建相当复杂的处理图片程 ...

  6. java jdk自带程序分析(内存分析/线程分析)

    周末看到一个用jstack查看死锁的例子.昨天晚上总结了一下jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令. 1.1.Jstack 1.1   jstack能得到运行jav ...

  7. boost在linux下的编译和使用

    上一篇boost在windows可以正常的使用了,但是在linux下不行. [尝试一:使用和windows同一套代码编译,编译时报错] 我是在Ubuntu使用共享文件夹的方式和windows使用的同一 ...

  8. Android UI-开源框架ImageLoader的完美例子

    Android开源框架ImageLoader的完美例子 2013年8月19日开源框架之Universal_Image_Loader学习 很多人都在讨论如何让图片能在异步加载更加流畅,可以显示大量图片, ...

  9. Android:布局实例之常见用户设置界面

    实现效果: 整理思路: 1.控件:文字TextView 和 右箭头ImageView 2.因为考虑到点击效果,设计为:最外层为全圆角,内层有四种情况,分别为上圆角.无圆角.下圆角和全圆角. 3.内层样 ...

  10. 初始化windows窗口

    LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { PAINTSTRUCT ps; switch (msg ...