设为首页 加入收藏

TOP

Android TabHost 详细讲解
2014-11-24 11:50:12 来源: 作者: 【 】 浏览:1
Tags:Android TabHost 详细 讲解



至于选项卡有什么好处或者用途,我想代码哥们都知道吧,我就不多说了。

二、在Android里面如何实现TabHost
有两种方式可以实现。

1、继承TabActivity,然后用getTabHost()方法获取TabHost,最后在布局文件中定义各个Tab选项卡添加到TabHost中





②:把Tab添加到TabHost中。




①:布局文件:


②:Activity




/**
*Demo2Activity.java
*2011-9-17 下午12:07:48
*Touch Android
*http://bbs.droidstouch.com
*/
package com.droidstouch.tabhost;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

/**
* @author Touch Android
*
*/
public class Demo2Activity extends TabActivity
{



protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// this.setContentView(R.layout.demo2); // 注意不要加上此行代码

//获取到TabHost对象
TabHost tabHost =this.getTabHost();

//把我们的布局文件添加到tabHost 的FrameLayout下面
LayoutInflater.from(this).inflate(R.layout.demo2, tabHost.getTabContentView(), true);



// 下面定义了两个选项卡

//获取一个新的TabHost.TabSpec,并关联到当前tab host
//参数:所需的选项卡标签
TabSpec pSpec = tabHost.newTabSpec("parent");
// 参数一:选项卡上的文字,参数二:选项卡的背景图片
pSpec.setIndicator("父类", this.getResources().getDrawable(R.drawable.f_root));
//设置选项卡内容
pSpec.setContent(R.id.tab1);


TabSpec subSpec = tabHost.newTabSpec("sub");
subSpec.setIndicator("子类", this.getResources().getDrawable(R.drawable.f_sub));
subSpec.setContent(R.id.tab2);


// 将选项卡添加到TabHost中
tabHost.addTab(pSpec);
tabHost.addTab(subSpec);

}

}

第二种方式
①:布局文件




< xml version="1.0" encoding="utf-8" >
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>




android:layout_width="fill_parent"
android:layout_height="fill_parent">

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>


android:layout_width="fill_parent"
android:layout_height="wrap_content"/>


android:layout_width="fill_parent"
android:layout_height="fill_parent">

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Touch Android"/>


android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="http://bbs.droidstouch.com"/>












②:Activity:


package com.droidstouch.tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Dome1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo1);




//从布局文件中 获取到TabHost
TabHost tabHost = (TabHost) this.findViewById(R.id.tabs);
//安装TabHost
tabHost.setup();


// 下面定义两个选项卡

//获取一个新的TabHost.TabSpec,并关联到当前tab host
//参数:所需的选项卡标签
TabSpec pSpec = tabHost.newTabSpec("parent");
pSpec.setIndicator("父类", this.getResources().getDrawable(R.drawable.f_root));
pSpec.setContent(R.id.txtV1);



TabSpec subSpec = tabHost.newTabSpec("sub");
subSpec.setIndicator("子类", this.getResources().getDrawable(R.drawable.f_root));
subSpec.setContent(R.id.txtV2);


//添加选项卡到TabHost中
tabHost.addTab(pSpec);
tabHost.addTab(subSpec);

}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android SurfaceView使用 笔记 下一篇Android ListView美化-->几个..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·深入理解 Java 集合 (2025-12-27 07:22:48)
·Java集合框架全面解 (2025-12-27 07:22:45)
·时隔 15 年,巨著《J (2025-12-27 07:22:43)
·定义一个类模板并实 (2025-12-27 06:52:28)
·一文搞懂怎么用C语言 (2025-12-27 06:52:25)