2010.11.11———Android TabHost选项卡组件

本文详细介绍了Android中TabHost组件的两种使用方式,并推荐了继承TabActivity的方法。通过实例展示了如何设置TabHost、TabWidget及内容布局,适用于希望了解TabHost具体实现方式的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2010.11.11———Android TabHost选项卡组件

参考:[url]http://www.cnblogs.com/keyindex/articles/1815074.html[/url]

其实 TabHost 有两种写法
1、继承Activity 用finViewById() 来得到TabHost
2、继承TabActivity 用getTabHost() 来得到TabHost


我建议用第二种

因为我用第一种时 遇到个问题

当我的tab页指向另一个activity 即用intent来传递时 就会报个错误
java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?


很是郁闷 所以 我建议用第二种


步骤:

[color=red]1、layout.xml 这个xml 因为是继承TabActivity 所以 有几个id是特定的 不能更改[/color]

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
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" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="68px">
<LinearLayout
android:id="@+id/gcxx"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first tab" />
</LinearLayout>

<LinearLayout
android:id="@+id/sbjl"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sbjl_upload_time"
/>

</LinearLayout>
</FrameLayout>
</TabHost>



TabHost
TabWidget  tab头
FrameLayout tab要显示的内容


[color=blue]这三个id是固定的 不能更改 [/color]


[color=red]2、Activity[/color]


package com.huitu.project;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TextView;

public class QueryResultActivity extends TabActivity {

private TextView tv_upload_time;
private TextView tv_problem;
private TextView tv_suggestion;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.query_result);

tv_upload_time = (TextView)findViewById(R.id.sbjl_upload_time);
tv_problem = (TextView)findViewById(R.id.sbjl_problem);
tv_suggestion = (TextView)findViewById(R.id.sbjl_suggestion);

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String GCBM = bundle.getString("GCBM");
String problem = bundle.getString("problem");
String suggestion = bundle.getString("suggestion");
String upload_time = bundle.getString("upload_time");

tv_upload_time.setText(upload_time);
tv_problem.setText(problem);
tv_suggestion.setText(suggestion);



TabHost tabHost = getTabHost();
//tabHost.setup(); //当继承Activity是 必须调用


Intent gcxx = new Intent(this,GCXXActivity.class);
gcxx.putExtra("GCBM", GCBM);

tabHost.addTab(tabHost.newTabSpec("gcxx").setIndicator("工程信息").setContent(gcxx));
tabHost.addTab(tabHost.newTabSpec("sbjl").setIndicator("上报记录").setContent(R.id.sbjl));
tabHost.addTab(tabHost.newTabSpec("pic").setIndicator("图片信息").setContent(R.id.gcxx));
tabHost.addTab(tabHost.newTabSpec("video").setIndicator("视频信息").setContent(R.id.sbjl));
tabHost.setCurrentTab(2);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值