the implementation of the options card feature in Android
Tabhost and Tabwidget are used in Android to implement tab functionality. Tabhost must be the root node of the layout, which contains two child nodes:
Tabwidget, Display tab;
Framelayout, displays the label contents.
There are two ways to implement tab functionality, one is to put multiple view in the same activity, and then use the tag to switch. The other is to switch between different activity directly using tags.
The latter method is more commonly used. This article also only introduces the implementation of the latter method of the process.
1. Create a project, the name can be called Hellotabwidget.
2. Create a number of different activity to represent different content in each tab.
3. Design different icons for the labels. Each label should have two icons, one for selection and one unchecked. Place the picture under the res/drawable/folder. Then create a corresponding
Statelistdrawable, used to enable direct automatic switching between checked and unchecked.
[Java]View Plaincopy
- <?xml version="1.0" encoding="Utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android" >
- <!--When selected, use grey-to-
- <item android:drawable="@drawable/ic_tab_artists_grey"
- android:state_selected="true"/>
- <!--when not selected, use white-->
- <item android:drawable="@drawable/ic_tab_artists_white"/>
- </selector>
4. Replace the main.xml with the following content.
[Java]View Plaincopy
- <?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"
- android:padding="5DP" >
- <tabwidget
- Android:id="@android: Id/tabs"
- Android:layout_width="Fill_parent"
- android:layout_height="Wrap_content"/>
- <framelayout
- Android:id="@android: Id/tabcontent"
- Android:layout_width="Fill_parent"
- android:layout_height="Fill_parent"
- android:padding="5DP"/>
- </LinearLayout>
- </TabHost>
5. Let your main activity inherit from the tabactivity.
6. Add tags to the OnCreate method of the main activity
[Java]View Plaincopy
- Public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Resources res = getresources (); //Resource object to get Drawables
- Tabhost tabhost = Gettabhost (); //The activity Tabhost
- Tabhost.tabspec spec; //resusable tabspec for each tab
- Intent Intent; //Reusable Intent for each tab
- //Create an Intent to launch a Activity for the tab (to be reused)
- Intent = New Intent (). SetClass (This, artistsactivity. Class);
- //Initialize a tabspec for per tab and add it to the Tabhost
- Spec = Tabhost.newtabspec ("artists"). Setindicator ("artists",
- Res.getdrawable (r.drawable.ic_tab_artists))
- . SetContent (Intent);
- Tabhost.addtab (spec);
- // do the same for the other tabs
- Intent = New Intent (). SetClass (This, albumsactivity. Class);
- Spec = Tabhost.newtabspec ("albums"). Setindicator ("albums",
- Res.getdrawable (R.drawable.ic_tab_albums))
- . SetContent (Intent);
- Tabhost.addtab (spec);
- Intent = New Intent (). SetClass (This, songsactivity. Class);
- Spec = Tabhost.newtabspec ("songs"). Setindicator ("Songs",
- Res.getdrawable (r.drawable.ic_tab_songs))
- . SetContent (Intent);
- Tabhost.addtab (spec);
- Tabhost.setcurrenttab (2);
- }
7. Run the program to see the effect.
Summarize:
Main.xml defines the style of the tab, which, together with tabactivity, creates a frame for the tab. Tabhost.tabspec represents the contents of a tab, and Tabhost uses the AddTab method to add it to the entire tab.
Tabhost.tabspec
Original: http://blog.csdn.net/cool_android/article/details/7202381
The implementation of the options card feature in Android