=======================================================================
1、在主activity中添加一個Button按鈕,利用按鈕跳轉到Tab標籤中
private Button tabViewButton = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tabViewButton = (Button)findViewById(R.id.tabViewButton); tabViewButton.setOnClickListener(new tabViewButtonListener()); } //利用intent對象轉到tab標籤的activity中 class tabViewButtonListener implements OnClickListener{ public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(TabViewTest.this, TabViewDemo.class); TabViewTest.this.startActivity(intent); } }
2、建立Tab的布局檔案,tab_value.xml
<FrameLayout xmlns:android="http://schemas.android.com/ apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="這個是第一個Tab標籤" /> <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="這個是第二個Tab標籤" /> <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="這個是第三個Tab標籤" /></FrameLayout>
3、建立Tab的activity,
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setTitle("TabViewDemo"); TabHost tabHost = getTabHost(); LayoutInflater.from(this).inflate(R.layout.tab_demo, tabHost.getTabContentView(),true); tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("Tab1"). setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("Tab2")setIndicator("Tab2"). setContent(R.id.view2)); tabHost.addTab(tabHost.newTabSpec("Tab3").setIndicator("Tab3"). setContent(R.id.view3)); }
4、在AdroidManifest.xml中註冊Tab的activity。
<activity android:name=".TabViewDemo" android:label="@string/app_view"> </activity>