首先在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;
}
}
這裡發下包圖
還有效果圖