tabhost的使用

TabHost是整個Tab的容器,包括兩部分,TabWidget和FrameLayout。TabWidget就是每個tab的標籤,FrameLayout則是tab內容。

1、如果我們使用extends TabAcitivty,如同ListActivity,TabHost必須設置爲@android:id/tabhost
2、TabWidget必須設置android:id爲@android:id/tabs
3、FrameLayout需要設置android:id爲@android:id/tabcontent
4、參考這兒:aspx">http://blog.csdn.net/flowingflying/archive/2011/04/06/6304289.aspx

先自定義一個xml文件:
Java代碼
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
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">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:paddingBottom="53px"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="50px"
android:visibility="gone"
android:layout_weight="0.0"/>
<RadioGroup
android:gravity="center_vertical"
android:orientation="horizontal"
android:id="@+id/main_radio"
android:background="@drawable/radiogroup_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_gravity="bottom">
<RadioButton
android:id="@+id/main_index_button"
android:layout_marginTop="1.0dip"
android:layout_marginRight="5dip"
android:text="@string/main_name"
android:drawableTop="@drawable/unistall"
style="@style/main_tab_bottom"
android:background="@drawable/radio_bg"/>
<RadioButton
android:id="@+id/main_running_button"
android:layout_marginTop="1.0dip"
android:layout_marginRight="5dip"
android:text="@string/run_manager_name"
android:drawableTop="@drawable/unistall"
style="@style/main_tab_bottom"
android:background="@drawable/radio_bg"/>
<RadioButton
android:id="@+id/main_uninstall_button"
android:layout_marginTop="1.0dip"
android:text="@string/uninstall_manager_name"
android:drawableTop="@drawable/unistall"
style="@style/main_tab_bottom"
android:background="@drawable/radio_bg"/>
</RadioGroup>
</LinearLayout>
</TabHost>

爲了讓tabHost顯示在下方,要將RadioGroup的layout_gravity設置爲bottom,再將FrameLayout的layout_weight設置爲1,這樣就可以將RadioGroup撐到最下方。style="@style/main_tab_bottom"裏面定義了樣式文件。

接下來就是在activity中初始化並添加tabhost:
Java代碼
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.addTab(Constant.tabHost.newTabSpec("Main")
.setIndicator(getString(R.string.main_name),null)
.setContent(new Intent(this, Main.class)));
tabHost.addTab(Constant.tabHost.newTabSpec("RunManager")
.setIndicator(getString(R.string.run_manager_name),null)
.setContent(new Intent(this, RunManager.class)));
tabHost.addTab(Constant.tabHost.newTabSpec("UninstallManager")
.setIndicator(getString(R.string.uninstall_manager_name),null)
.setContent(new Intent(this, UninstallManager.class)));


初始化每個RadioButton併爲其添加setOnCheck

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章