7. 在本文开始出,曾经提到“或者是通过 Intent 对象启动的 Activity ”,也就是说 setContent 的参数可以是一个 Intent 对象,然后用该对象启动另外一个 Activity 。为此我们先创建一个 Activity 类,并为它指定相应的 layout 。假定我们让第三个 tab 显示的内容为 Intent 对象所指定的 Activity 。 Layout 内容如下:
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
xmlns:Android = "http://schemas.android.com/apk/res/android"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" >
< TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Hello, Android"
android:textSize = "30px"
android:textColor = "#FF0" />
</ LinearLayout >
对应的 Activity 代码如下:
package com.pat.gui;
import android.app.Activity;
import android.os.Bundle;
public class ThirdTab extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.thirdtab);
}
}
在 AndroidManifest.xml 中的 Application 标签内,增加对 ThirdTab 的描��:
<activity android:name=".ThirdTab" />
8. 现在,咱们来修改 AndroidGUI24Activity 的代码,使之如下 ( 注意粗体字部分的代码变化 ) :
package com.pat.gui;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
public class AndroidGUI24Activity extends TabActivity
implements
OnClickListener
{
private TabHost tabhost;
private Resources res;
private Button tab1btn1;
private Button tab2btn1;
private Button tab2btn2;
@Override
public void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
// 获取该 Activity 用于容纳 tab 的 TabHost 对象
// Returns the TabHost the activity is using to host its tabs.
tabhost = getTabHost();
res = getResources();
// 获取 LayoutInflater 对象
LayoutInflater inflater = LayoutInflater.from (this );
// TabHost.getTabContentView 方法的作用: Get the FrameLayout which holds tab content
// LayoutInflater.inflate 方法的作用:
// Inflate a new view hierarchy from the specified xml resource.
// 其原型为: public View inflate (int resource, ViewGroup root)
// 参数:
// resource ID for an XML layout resource to load (e.g., R.layout.main_page)
// root Optional view to be the parent of the generated hierarchy.
// 返回值:
// The root View of the inflated hierarchy. If root was supplied, this is the root
// View; otherwise it is the root of the inflated XML file.
// 下面这几句话,就是将 tab1.xml 、 tab2.xml 的内容,全嵌入到 tabhost.getTabContentView()
// 所返回的 FrameLayout 中。这次我们仅 inflate 两个 tab 的内容,第三个 tab 将有一个 Intent 对象指定
inflater.inflate(R.layout.tab1 , tabhost.getTabContentView());
inflater.inflate(R.layout.tab2 , tabhost.getTabContentView());
// 给 tabhost 增加 tab
// newTabSpec(String tag) 返回一个 TabHost.TabSpec 对象, TabHost.TabSpec
// setIndicator(" 国籍 "): 显示 tab 上的文字
// setContent(int id) :指定 tab 的内容
tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator(" 国籍 ", res.getDrawable(R.drawable.cn )).setContent(R.id.tab1 ));
tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator(" 爱好 ", res.getDrawable(R.drawable.basketball )).setContent(R.id.tab2 ));
tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator(" 系统 ",res.getDrawable(R.drawable.amplifer_1 ))
.setContent(new Intent(this , ThirdTab.class ) ));
// 指定显示第几个 tab
tabhost.setCurrentTab(1);
// 在上面的工作都做完后,调用 setContentView
//setContentView(R.layout.main);
setContentView(tabhost);
// 获取 6 个按钮对象,并分别给它们增加 OnClickListener
tab1btn1 = (Button)findViewById(R.id.tab1btn1 );
tab1btn1.setOnClickListener(this );
tab2btn1 = (Button)findViewById(R.id.tab2btn1 );
tab2btn2 = (Button)findViewById(R.id.tab2btn2 );
tab2btn1.setOnClickListener(this );
tab2btn2.setOnClickListener(this );
}
//@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.tab1btn1 :
tabhost.setCurrentTab(1); // 跳转到第二个 tab
break ;
case R.id.tab2btn1 :
tabhost.setCurrentTab(0); // 跳转到第一个 tab
break ;
case R.id.tab2btn2 :
tabhost.setCurrentTab(2); // 跳转到第三个 tab
break ;
}
}
}
运行结果如下 ( 点击“系统”这个 tab) :
点击“国籍”、“爱好”和“系统”,各 tab 之间可以进行很好的切换。
