=======================================================================
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>