<TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" 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="fill_parent"> </FrameLayout> </LinearLayout> </TabHost>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="界面1"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="欢迎来到界面1"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="界面2"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="欢迎来到界面2"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="界面3"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="欢迎来到界面3"/> </LinearLayout>
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; public class MainActivity extends Activity { private TabHost tabHost;//声明TabHost组件的对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tabHost=(TabHost)findViewById(android.R.id.tabhost);//获取tabHost对象 tabHost.setup();//初始化TabHost组件 LayoutInflater inflater=LayoutInflater.from(this);//声明并实例化一个LayoutInflater对象 //关于LayoutInflater详细,请看我的另外一篇转载的总结 inflater.inflate(R.layout.tab1, tabHost.getTabContentView()); inflater.inflate(R.layout.tab2, tabHost.getTabContentView()); inflater.inflate(R.layout.tab3, tabHost.getTabContentView()); tabHost.addTab(tabHost.newTabSpec("tab01") .setIndicator("标签页一") .setContent(R.id.linearLayout1));//添加第一个标签页 tabHost.addTab(tabHost.newTabSpec("tab02") .setIndicator("标签页二") .setContent(R.id.linearLayout2));//添加第二个标签页 tabHost.addTab(tabHost.newTabSpec("tab03") .setIndicator("标签页三") .setContent(R.id.linearLayout3));//添加第三个标签页 } }
标签将放在<FrameLayout/>标签下
转载请注明出处:http://blog.csdn.net/acmman/article/details/44904205