标签显示界面的主要特点是可以在一个窗口中显示多组标签栏的类容。

在Android系统中,每个标签栏称为一个Tab,而包含多个标签栏的内容就称为TabHost。

通过TabHost的继承结构来看,TabHost类是FrameLayout的子类。

实现标签显示界面有两种方式可供选择。

  1. 直接让一个Activity继承TabActivity类。
  2. 利用findViewById()方法取得TabHost组件,并进行一些配置。

下面我们用两个简单例子来体验一下:

第一种继承TabActivity类:(注意:TabActivity类已被官方提出抛弃)

首先就是布局文件tab.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_edit"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入检索关键字..."
android:textSize="18px" />
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_clock"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<AnalogClock
android:id="@+id/myAnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_sex"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/male"
android:checked="true"
android:text="性别:男" />
<RadioButton
android:id="@+id/female"
android:text="性别:女" />
</RadioGroup>
</LinearLayout>
</LinearLayout>

其次就是默认的MainActivity的实现:

 public class MyTabHostDemo extends TabActivity { // 继承了TabActivity
private TabHost myTabHost;
private int[] layRes = new int[] { R.id.tab_edit, R.id.tab_clock,
R.id.tab_sex }; // 这些是内嵌布局文件的ID @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.myTabHost = super.getTabHost(); // 取得TabHost对象
LayoutInflater.from(this).inflate(R.layout.tab,
this.myTabHost.getTabContentView(), true); // true表示实例化布局文件中的组件
for (int x = 0; x < this.layRes.length; x++) {
TabSpec myTab = this.myTabHost.newTabSpec("tab" + x) ;
myTab.setIndicator("标签 - " + x) ;
myTab.setContent(this.layRes[x]) ;
this.myTabHost.addTab(myTab) ;
}
}
}

到此,一个简单的TabHost的使用就完成了。

第二种方式的实现:

首先,我们还是是布局文件的实现。

tab.xml:

 <?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TabHost01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" >
</TextView>
</LinearLayout> <LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <TextView
android:id="@+id/TextView02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="two" >
</TextView>
</LinearLayout> <LinearLayout
android:id="@+id/LinearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <TextView
android:id="@+id/TextView03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="three" >
</TextView>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</LinearLayout> </TabHost>

main.xml:(这个不是默认.JAVA文件加载的布局文件,只是为了方便没有新建布局文件。)

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nihao!!!"/> </LinearLayout>

实现默认的MainActivity:

 public class MainActivity extends ActivityGroup {//ActivityGroup类也已被建议遗弃
private TabHost tabHost;
Intent oneIntent; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
oneIntent = new Intent();
oneIntent.setClass(this, testActivity.class);
tabHost = (TabHost) this.findViewById(R.id.TabHost01);//获得对象
tabHost.setup();//建立对象(与上一行代码必须同时存在)
tabHost.setup(this.getLocalActivityManager());//要实现Intent跳转,必须要这句话。
try { tabHost.addTab(tabHost
.newTabSpec("tab_1")
.setContent(R.id.LinearLayout1)
.setIndicator("TAB1",
this.getResources().getDrawable(R.drawable.img1)));
tabHost.addTab(tabHost
.newTabSpec("tab_2")
.setContent(R.id.LinearLayout2)
.setIndicator("TAB2",
this.getResources().getDrawable(R.drawable.img2)));
tabHost.addTab(tabHost.newTabSpec("tab_3")
.setContent(R.id.LinearLayout3).setIndicator("TAB3"));
tabHost.addTab(tabHost
.newTabSpec("tab_4")
.setIndicator("TAB4",
this.getResources().getDrawable(R.drawable.img3))
.setContent(oneIntent));
tabHost.setCurrentTab(1); // 设置默认的页面
} catch (Exception ex) {
ex.printStackTrace();
Log.d("EXCEPTION", ex.getMessage());
}
} }

新建一个用于Intent跳转的Activity:

 public class testActivity extends Activity {

     @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView) findViewById(R.id.TV);
tv.setText("已经跳转了");
} }

接下来运行即可。

这样,用两个例子分别实现了两种不同的实现方式。

Android TabHost的使用的更多相关文章

  1. Android tabhost下的activity怎样获取传来的值

    android tabhost下的activity怎样获取传来的值,具体解决方案如下: 解决方案: 其他activity设置intent:Intent intent=new Intent(); int ...

  2. Android - TabHost 与 Fragment 制作页面切换效果

    Android - TabHost 与 Fragment 制作页面切换效果 Android API 19 , API 23 三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义 ...

  3. Android——TabHost(标签容器)相关知识总结贴

    android 2.3 r1 中文 api (58) —— TabHost http://www.apkbus.com/android-18911-1-1.html   android中文api (5 ...

  4. Android TabHost使用

    TabHost是Android中自带的选项卡控件,效果图如下: 主布局文件 <RelativeLayout xmlns:android="http://schemas.android. ...

  5. Android -- TabHost

    TabHost 也就相当于Windows下的选项框 有两种实现方式 1.  继承TabActivity (已经废弃):从TabActivity中用getTabHost()方法获取TabHost 2. ...

  6. Android -- TabHost、Fragment、状态保存、通信

    工程结构                                                                                       TabAFm到Ta ...

  7. Android TabHost中Activity之间传递数据

    例子1: TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost); tabhost.setup(this.getLocalActi ...

  8. Android TabHost 动态修改图标或者动态改变标题

    那时客户需要实现在TabHost标题上动态显示从数据库获取的个数.起初这样思考的,从数据库 获取个数是非常简单,但是要把获取的个数显示在TabHost标题,思前想后,想用Handler来异步实现消息传 ...

  9. Android TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题

    最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...

  10. Android TabHost TabWidget 去除黑线(底部下划线)

    采用TabHost布局时,往往会发现默认的系统风格与软件风格很不协调,比如TabWidget的下划线影响布局效果.通常情况下会去除其下划线.如果是采用xml布局文件,在TabWidget的属性项设置a ...

随机推荐

  1. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.1

    For fixed basis of in $\scrH$ and $\scrK$, the matrix $A^*$ is the conjugate transpose of the matrix ...

  2. QT学习(对话框)codeblock版本

    参考: http://www.cnblogs.com/JohnShao/archive/2011/09/26/2191627.html http://www.cnblogs.com/xiao-chen ...

  3. 经典sql总结(1)

    1.表示info 信息,字段为Id和res,如何得到如下结果

  4. bzoj 1951 [Sdoi2010]古代猪文(数论知识)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1951 [思路] 一道优(e)秀(xin)的数论题. 首先我们要求的是(G^sigma{ ...

  5. Storm系列(四)Topology提交校验过程

    功能:提交一个新的Topology,并为Topology创建storm-id(topology-id),校验其结构,设置必要的元数据,最后为Topology分配任务. 实现源码: 1  ); Conf ...

  6. 获取所有组合算法、获取全排列算法(java)

    转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5574516.html 受到ACM1015的影响,个人感觉,有必要对统计学上的 全组合和全排列 进行一个简单的总结 ...

  7. Codeforces2B - The least round way(DP)

    题目大意 给定一个N*N的格子,每个格子里有一个非负数,要求你找出从左上角到右下角的一条路径,使得它满足路径上的格子里的数全部乘起来的积尾部0最少 题解 如果要产生0肯定是2*5得出来的,最终的乘积可 ...

  8. 解决android锁屏或解锁后activity重启的问题

    If your target build version is Honeycomb 3.2 (API Level 13) or higher you must put the screenSize f ...

  9. makeKeyAndVisible的功能

    makeKeyAndVisible的作用 [self.window makeKeyAndVisible] 由于iPhone是单窗口程序,所以也就只有这么一个Window对象,而且是UIWindow,不 ...

  10. Fast特征检测

    一.Fast算法 1.基本原理 Fast特征点检测feature2D原理是在圆周上按顺时针方向从1到16的顺序对圆周像素点进行编号.如果在圆周上有N个连续的像素的亮度都比圆心像素的亮度Ip加上阈值t还 ...