首先在xml文件中做好布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_maintabhost">
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="55px">
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tab1"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50px" />
</RelativeLayout>
</TabHost>
</LinearLayout>
再写一个mainActivity里面包含了4个Activity 4个Activity里面各只有一个oncreate还有一个布局的话也是自动生成的一个Textview 这里就不写了
下面就是主要的TabHost
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 取得TabHost
TabHost tabhost = (TabHost) findViewById(R.id.tabhost);
// 设置了TabHost的背景颜色 蓝色
tabhost.setBackgroundColor(Color.argb(150, 20, 80, 150));
tabhost.setup();
LayoutInflater inflater = LayoutInflater.from(this);
// 把配置文件转换为显示TabHost内容的FrameLayout中的层级
inflater.inflate(R.layout.inspection, tabhost.getTabContentView());
inflater.inflate(R.layout.inventory, tabhost.getTabContentView());
inflater.inflate(R.layout.label, tabhost.getTabContentView());
inflater.inflate(R.layout.mytask, tabhost.getTabContentView());
// 设置"任务"标签
TabSpec spec1 = tabhost.newTabSpec("我的任务").setIndicator("我的任务");
// 设置"任务"模块显示内容
spec1.setContent(R.id.my_task);
tabhost.addTab(spec1);
TabSpec spec2 = tabhost.newTabSpec("盘点").setIndicator("盘点");
spec2.setContent(R.id.my_inventory);
tabhost.addTab(spec2);
TabSpec spec3 = tabhost.newTabSpec("巡查").setIndicator("巡查");
spec3.setContent(R.id.my_inspection);
tabhost.addTab(spec3);
TabSpec spec4 = tabhost.newTabSpec("贴标签").setIndicator("贴标签");
spec4.setContent(R.id.my_label);
tabhost.addTab(spec4);
}
private TabHost findViewById(Class<id> class1) {
// TODO Auto-generated method stub
return null;
}
}
这里发下包图
还有效果图