The implementation of the options card feature in Android

Source: Internet
Author: User

 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
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <!--When selected, use grey-to-
  4. <item android:drawable="@drawable/ic_tab_artists_grey"
  5. android:state_selected="true"/>
  6. <!--when not selected, use white-->
  7. <item android:drawable="@drawable/ic_tab_artists_white"/>
  8. </selector>

4. Replace the main.xml with the following content.

[Java]View Plaincopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <tabhost xmlns:android="Http://schemas.android.com/apk/res/android"
  3. Android:id="@android: Id/tabhost"
  4. Android:layout_width="Fill_parent"
  5. android:layout_height="Fill_parent" >
  6. <linearlayout
  7. android:orientation="Vertical"
  8. Android:layout_width="Fill_parent"
  9. android:layout_height="Fill_parent"
  10. android:padding="5DP" >
  11. <tabwidget
  12. Android:id="@android: Id/tabs"
  13. Android:layout_width="Fill_parent"
  14. android:layout_height="Wrap_content"/>
  15. <framelayout
  16. Android:id="@android: Id/tabcontent"
  17. Android:layout_width="Fill_parent"
  18. android:layout_height="Fill_parent"
  19. android:padding="5DP"/>
  20. </LinearLayout>
  21. </TabHost>

5. Let your main activity inherit from the tabactivity.

6. Add tags to the OnCreate method of the main activity

[Java]View Plaincopy
  1. Public void OnCreate (Bundle savedinstancestate) {
  2. super.oncreate (savedinstancestate);
  3. Setcontentview (R.layout.main);
  4. Resources res = getresources (); //Resource object to get Drawables
  5. Tabhost tabhost = Gettabhost (); //The activity Tabhost
  6. Tabhost.tabspec spec; //resusable tabspec for each tab
  7. Intent Intent; //Reusable Intent for each tab
  8. //Create an Intent to launch a Activity for the tab (to be reused)
  9. Intent = New Intent (). SetClass (This, artistsactivity.   Class);
  10. //Initialize a tabspec for per tab and add it to the Tabhost
  11. Spec = Tabhost.newtabspec ("artists"). Setindicator ("artists",
  12. Res.getdrawable (r.drawable.ic_tab_artists))
  13. . SetContent (Intent);
  14. Tabhost.addtab (spec);
  15. // do the same for the other tabs
  16. Intent = New Intent (). SetClass (This, albumsactivity.   Class);
  17. Spec = Tabhost.newtabspec ("albums"). Setindicator ("albums",
  18. Res.getdrawable (R.drawable.ic_tab_albums))
  19. . SetContent (Intent);
  20. Tabhost.addtab (spec);
  21. Intent = New Intent (). SetClass (This, songsactivity.   Class);
  22. Spec = Tabhost.newtabspec ("songs"). Setindicator ("Songs",
  23. Res.getdrawable (r.drawable.ic_tab_songs))
  24. . SetContent (Intent);
  25. Tabhost.addtab (spec);
  26. Tabhost.setcurrenttab (2);
  27. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.