`
熊滔爱孟涛静
  • 浏览: 121721 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

TabHost

阅读更多

Android TabWidget/TabHost有两种使用方法:

第一种:使用系统自带写好的TabHost(及继承自TabActivity类)具体代码如下:

Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">   
  5.     <LinearLayout android:id="@+id/tab1"  
  6.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  7.         androidrientation="vertical">   
  8.         <TextView android:id="@+id/TextView1"  
  9.             android:text="This is a tab1" android:layout_width="fill_parent"  
  10.             android:layout_height="wrap_content">   
  11.         </TextView>   
  12.     </LinearLayout>   
  13.     <LinearLayout android:id="@+id/tab2"  
  14.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  15.         androidrientation="vertical">   
  16.         <TextView android:id="@+id/TextView2"  
  17.             android:text="This is a tab2" android:layout_width="fill_parent"  
  18.             android:layout_height="wrap_content">   
  19.         </TextView>   
  20.     </LinearLayout>   
  21.     <LinearLayout android:id="@+id/tab3"  
  22.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  23.         androidrientation="vertical">   
  24.         <TextView android:id="@+id/TextView3"  
  25.             android:text="This is a tab3" android:layout_width="fill_parent"  
  26.             android:layout_height="wrap_content">   
  27.         </TextView>   
  28.     </LinearLayout>   
  29. </FrameLayout>  
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout android:id="@+id/tab1"
		android:layout_width="fill_parent" android:layout_height="fill_parent"
		androidrientation="vertical">
		<TextView android:id="@+id/TextView1"
			android:text="This is a tab1" android:layout_width="fill_parent"
			android:layout_height="wrap_content">
		</TextView>
	</LinearLayout>
	<LinearLayout android:id="@+id/tab2"
		android:layout_width="fill_parent" android:layout_height="fill_parent"
		androidrientation="vertical">
		<TextView android:id="@+id/TextView2"
			android:text="This is a tab2" android:layout_width="fill_parent"
			android:layout_height="wrap_content">
		</TextView>
	</LinearLayout>
	<LinearLayout android:id="@+id/tab3"
		android:layout_width="fill_parent" android:layout_height="fill_parent"
		androidrientation="vertical">
		<TextView android:id="@+id/TextView3"
			android:text="This is a tab3" android:layout_width="fill_parent"
			android:layout_height="wrap_content">
		</TextView>
	</LinearLayout>
</FrameLayout>


Java代码 复制代码
  1. package com.Aina.Android;   
  2.   
  3. import android.app.AlertDialog;   
  4. import android.app.Dialog;   
  5. import android.app.TabActivity;   
  6. import android.content.DialogInterface;   
  7. import android.os.Bundle;   
  8. import android.view.LayoutInflater;   
  9. import android.widget.TabHost;   
  10.   
  11. public class Test_TabWidget extends TabActivity {   
  12.     /** Called when the activity is first created. */  
  13.     private TabHost tabHost;   
  14.   
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {   
  17.         super.onCreate(savedInstanceState);   
  18.         // setContentView(R.layout.main);   
  19.         tabHost = this.getTabHost();   
  20.         LayoutInflater li = LayoutInflater.from(this);   
  21.         li.inflate(R.layout.main, tabHost.getTabContentView(), true);   
  22.         tabHost.addTab(tabHost.newTabSpec("Tab_1").setContent(R.id.tab1)   
  23.                 .setIndicator("TAB1",   
  24.                         this.getResources().getDrawable(R.drawable.img1)));   
  25.         tabHost.addTab(tabHost.newTabSpec("Tab_2").setContent(R.id.tab2)   
  26.                 .setIndicator("TAB2",   
  27.                         this.getResources().getDrawable(R.drawable.img2)));   
  28.         tabHost.addTab(tabHost.newTabSpec("Tab_3").setContent(R.id.tab3)   
  29.                 .setIndicator("TAB3",   
  30.                         this.getResources().getDrawable(R.drawable.img3)));   
  31.         tabHost.setCurrentTab(1);   
  32. //      tabHost.setBackgroundColor(Color.GRAY);   
  33.         tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {   
  34.   
  35.             public void onTabChanged(String tabId) {   
  36.                 Dialog dialog = new AlertDialog.Builder(Test_TabWidget.this)   
  37.                         .setTitle("提示").setMessage(   
  38.                                 "选中了" + tabId + "选项卡").setIcon(R.drawable.icon).setPositiveButton("确定"new DialogInterface.OnClickListener(){   
  39.   
  40.                                     public void onClick(DialogInterface dialog,   
  41.                                             int which) {   
  42.                                         // TODO Auto-generated method stub   
  43.                                            
  44.                                     }   
  45.                                        
  46.                                 }).create();   
  47.                 dialog.show();   
  48.   
  49.             }   
  50.   
  51.         });   
  52.     }   
  53. }  
package com.Aina.Android;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

public class Test_TabWidget extends TabActivity {
	/** Called when the activity is first created. */
	private TabHost tabHost;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		tabHost = this.getTabHost();
		LayoutInflater li = LayoutInflater.from(this);
		li.inflate(R.layout.main, tabHost.getTabContentView(), true);
		tabHost.addTab(tabHost.newTabSpec("Tab_1").setContent(R.id.tab1)
				.setIndicator("TAB1",
						this.getResources().getDrawable(R.drawable.img1)));
		tabHost.addTab(tabHost.newTabSpec("Tab_2").setContent(R.id.tab2)
				.setIndicator("TAB2",
						this.getResources().getDrawable(R.drawable.img2)));
		tabHost.addTab(tabHost.newTabSpec("Tab_3").setContent(R.id.tab3)
				.setIndicator("TAB3",
						this.getResources().getDrawable(R.drawable.img3)));
		tabHost.setCurrentTab(1);
//		tabHost.setBackgroundColor(Color.GRAY);
		tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

			public void onTabChanged(String tabId) {
				Dialog dialog = new AlertDialog.Builder(Test_TabWidget.this)
						.setTitle("提示").setMessage(
								"选中了" + tabId + "选项卡").setIcon(R.drawable.icon).setPositiveButton("确定", new DialogInterface.OnClickListener(){

									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub
										
									}
									
								}).create();
				dialog.show();

			}

		});
	}
}



第二种:就是定义我们自己的tabHost:不用继承TabActivity,具体代码如下:

Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/TabHost01" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">   
  5.     <LinearLayout android:layout_width="fill_parent"  
  6.         android:orientation="vertical" android:layout_height="fill_parent">   
  7.         <TabWidget android:id="@android:id/tabs"  
  8.             android:layout_width="fill_parent"  
  9.             android:layout_height="wrap_content" />   
  10.         <FrameLayout android:id="@android:id/tabcontent"  
  11.             android:layout_width="fill_parent"  
  12.             android:layout_height="fill_parent">   
  13.             <LinearLayout android:id="@+id/LinearLayout1"  
  14.                 android:layout_width="fill_parent"  
  15.                 android:layout_height="wrap_content">   
  16.                 <TextView android:text="one"  
  17.                     android:id="@+id/TextView01" android:layout_width="wrap_content"  
  18.                     android:layout_height="wrap_content">   
  19.                 </TextView>   
  20.             </LinearLayout>   
  21.             <LinearLayout android:id="@+id/LinearLayout2"  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content">   
  24.                 <TextView android:text="two"  
  25.                     android:id="@+id/TextView02" android:layout_width="fill_parent"  
  26.                     android:layout_height="wrap_content">   
  27.                 </TextView>   
  28.             </LinearLayout>   
  29.             <LinearLayout android:id="@+id/LinearLayout3"  
  30.                 android:layout_width="wrap_content"  
  31.                 android:layout_height="wrap_content">   
  32.                 <TextView android:text="three"  
  33.                     android:id="@+id/TextView03" android:layout_width="fill_parent"  
  34.                     android:layout_height="wrap_content">   
  35.                 </TextView>   
  36.             </LinearLayout>   
  37.         </FrameLayout>   
  38.     </LinearLayout>   
  39. </TabHost>  
<?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">
	<LinearLayout android:layout_width="fill_parent"
		android:orientation="vertical" 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="fill_parent">
			<LinearLayout android:id="@+id/LinearLayout1"
				android:layout_width="fill_parent"
				android:layout_height="wrap_content">
				<TextView android:text="one"
					android:id="@+id/TextView01" android:layout_width="wrap_content"
					android:layout_height="wrap_content">
				</TextView>
			</LinearLayout>
			<LinearLayout android:id="@+id/LinearLayout2"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content">
				<TextView android:text="two"
					android:id="@+id/TextView02" android:layout_width="fill_parent"
					android:layout_height="wrap_content">
				</TextView>
			</LinearLayout>
			<LinearLayout android:id="@+id/LinearLayout3"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content">
				<TextView android:text="three"
					android:id="@+id/TextView03" android:layout_width="fill_parent"
					android:layout_height="wrap_content">
				</TextView>
			</LinearLayout>
		</FrameLayout>
	</LinearLayout>
</TabHost>


Java代码 复制代码
  1. package com.Aina.Android;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.util.Log;   
  6. import android.view.LayoutInflater;   
  7. import android.widget.TabHost;   
  8.   
  9. public class Test_TabHost extends Activity {   
  10.     /** Called when the activity is first created. */  
  11.     private TabHost tabHost;   
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {   
  15.         super.onCreate(savedInstanceState);   
  16.         setContentView(R.layout.main);   
  17.         try{   
  18.             tabHost = (TabHost) this.findViewById(R.id.TabHost01);   
  19.             tabHost.setup();   
  20.                
  21.             tabHost.addTab(tabHost.newTabSpec("tab_1")   
  22.                     .setContent(R.id.LinearLayout1)   
  23.                     .setIndicator("TAB1",this.getResources().getDrawable(R.drawable.img1)));   
  24.             tabHost.addTab(tabHost.newTabSpec("tab_2")   
  25.                     .setContent(R.id.LinearLayout2).setIndicator("TAB2",   
  26.                             this.getResources().getDrawable(R.drawable.img2)));   
  27.             tabHost.addTab(tabHost.newTabSpec("tab_3")   
  28.                     .setContent(R.id.LinearLayout3).setIndicator("TAB3",   
  29.                             this.getResources().getDrawable(R.drawable.img3)));   
  30.             tabHost.setCurrentTab(1);   
  31.         }catch(Exception ex){   
  32.             ex.printStackTrace();   
  33.             Log.d("EXCEPTION", ex.getMessage());   
  34.         }   
  35.   
  36.     }   
  37. }  
package com.Aina.Android;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.TabHost;

public class Test_TabHost extends Activity {
	/** Called when the activity is first created. */
	private TabHost tabHost;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		try{
			tabHost = (TabHost) this.findViewById(R.id.TabHost01);
			tabHost.setup();
			
			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",
							this.getResources().getDrawable(R.drawable.img3)));
			tabHost.setCurrentTab(1);
		}catch(Exception ex){
			ex.printStackTrace();
			Log.d("EXCEPTION", ex.getMessage());
		}

	}
}



注意:第二种方法时布局文件中的TabWidget的id必须定义为:android:id="@android:id/tabs",FrameLayout的id必须定义为:android:id="@android:id/tabcontent" 其它控件没有限制,否则报错。

分享到:
评论
1 楼 gf_crazy 2011-09-27  

刚好找第二种,其他地方全是第一种。

相关推荐

Global site tag (gtag.js) - Google Analytics