Android TabHost and androidtabhost
TabHost is a self-contained tab control in Android, as shown below:
Main layout File
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TabHost android:id="@+id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </FrameLayout> </LinearLayout> </TabHost></RelativeLayout>
Core code:
Package com. tabhost; import android. OS. bundle; import android. app. activity; import android. app. activityGroup; import android. content. intent; import android. view. menu; import android. view. window; import android. widget. tabHost; public class MainActivity extends ActivityGroup {private TabHost mTabHost; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); // set TabHost initTabs ();} private void initTabs () {mTabHost = (TabHost) findViewById (R. id. tabhost); mTabHost. setup (this. getLocalActivityManager (); // Add the log List tab. Pay attention to the Code in the following setContent. mTabHost is the key to achieving this requirement. addTab (mTabHost. newTabSpec ("tab_log "). setIndicator ("log", getResources (). getDrawable (R. drawable. login_bg )). setContent (new Intent (this, LogActivity. class); // Add the application settings tab. Pay attention to the Code in the following setContent. mTabHost is the key to achieving this requirement. addTab (mTabHost. newTabSpec ("tab_setting "). setIndicator ("set", getResources (). getDrawable (R. drawable. ic_launcher )). setContent (new Intent (this, SettingActivity. class); mTabHost. setCurrentTab (1 );}}
The two sub-pages are simple, with two activities and corresponding la S.