`

2010.11.11———Android TabHost选项卡组件

阅读更多
2010.11.11———Android TabHost选项卡组件

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

其实 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)'?


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


步骤:

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

<?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要显示的内容


这三个id是固定的 不能更改


2、Activity


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);
	}

}


分享到:
评论
3 楼 JACKDG2010 2011-07-29  
 
2 楼 lipeng88213 2011-04-08  
icoo1985 写道
用第一种方法也行,楼主你需要这么写就不会报那个错误啦^_^
public class Activity extends ActivityGroup{
...
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);

tabHost.setup(this.getLocalActivityManager());

...
}

恩 谢谢楼上 呵呵 不过现在没有代码 不好试 呵呵
1 楼 icoo1985 2011-04-07  
用第一种方法也行,楼主你需要这么写就不会报那个错误啦^_^
public class Activity extends ActivityGroup{
...
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);

tabHost.setup(this.getLocalActivityManager());

...
}

相关推荐

Global site tag (gtag.js) - Google Analytics