First, the tab
Andrews four steps to create a tab:
1, add TabHost, TabWidget and TabContent component in the layout file
Use TabHost integrally comprises up, and then use linear vertical layout manager TabWidget added thereto and the frame assembly layout manager FrameLayout
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <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="wrap_content" android:layout_height="wrap_content"> </FrameLayout> </LinearLayout> </TabHost>
2, written in XML layout files for each tab
tab1.xml: linear layout manager ImageView add a component, filling the src image without stretching.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/left" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/biaoqian_left"> </ImageView> </LinearLayout>
tab2.xml: same tab1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/right" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/biaoqian_right"> </ImageView> </LinearLayout>
3, obtain and initialize components TabHost
TabHost = TabHost the findViewById (android.R.id.tabhost);
tabHost.setup (); // initialize
4, add individual tabs to TabHost objects
// Use LayoutInflater object inflate method to add tabs,
LayoutInflater LayoutInflater = LayoutInflater.from (the this );
layoutInflater.inflate (R.layout.tab1, tabHost.getTabContentView ());
layoutInflater.inflate (R.layout.tab2, TabHost .getTabContentView ());
// use addTab method TabHost object to load the corresponding tab
tabHost.addTab (tabHost.newTabSpec ( "tab1") setIndicator ( " featured pictures." ) .setContent (R.id.left)) ;
tabHost.addTab (tabHost.newTabSpec ( "TAB2") setIndicator ( "Submission picture") .setContent (R.id.right).) ;
Final Results: