android TabHost小結 – Android移動開發技術文章_手機開發 Android移動開發教學課程

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

You May Also Like