Tabhost Introduction to Android music idioms and the use of fragment replacement tabactivity

Source: Internet
Author: User

Tabhost Introduction
The Tabhost component can store multiple tabs in the interface, and many of the software uses a modified component for design;
1. Tabhost Common Components
Tabwidget: This component is the Tabhost tab in the upper or lower buttons, you can click the button Switch tab;
Tabspec: Represents the Tab interface, add a tabspec can be added to the tabhost;
--Create Tab: Newtabspec (String tag), create a tab;
--Add Tab: AddTab (Tabspec);

Please see http://blog.csdn.net/harvic880925/article/details/17120325/for the use of tabhost.

Since Android3.2, tabactibvity is deprecated (Deprecated) and replaced by fragmentactivity, because fragment is more flexible than activiy, consumes less resources, Fully able to meet the tabactivity effect, so directly replace it. The original tabactibvity+tabhost+activity can also be used, but it is strongly recommended to use Fragmentactivity+fragmenttabhost+fragement

Fragmenttabhost usage:

1. Define the layout of the fragmentactivity:

[HTML] view Plaincopy

  1. <? XML version = "1.0" encoding = "Utf-8"?>
  2. < LinearLayout xmlns:android = "Http://schemas.android.com/apk/res/android"
  3. Android:layout_width = "Fill_parent"
  4. Android:layout_height = "Fill_parent"
  5. Android:orientation = "vertical" >
  6. < Framelayout
  7. Android:id = "@+id/realtabcontent"
  8. Android:layout_width = "Fill_parent"
  9. Android:layout_height = "0dip"
  10. Android:layout_weight = "1"/>
  11. < Android.support.v4.app.FragmentTabHost
  12. Android:id = "@android: Id/tabhost"
  13. Android:layout_width = "Fill_parent"
  14. Android:layout_height = "Wrap_content"
  15. Android:background = "@drawable/maintab_toolbar_bg" >
  16. < Framelayout
  17. Android:id = "@android: Id/tabcontent"
  18. Android:layout_width = "0DP"
  19. Android:layout_height = "0DP"
  20. Android:layout_weight = "0"/>
  21. </android.support.v4.app.FragmentTabHost >
  22. </linearlayout >

2. Must inherit Fragmentactivity

[Java] view plaincopy

    1. public class Maintabactivity extends fragmentactivity{
    2. Defining Fragmenttabhost Objects
    3. Private Fragmenttabhost Mtabhost;

3. Get Fragmenttabhost Object

[Java] view plaincopy

    1. Mtabhost = (fragmenttabhost) Findviewbyid (Android. R.id.tabhost);

4. Initializing the Fragmenttabhost object

[Java] view plaincopy

    1. Mtabhost.setup (this, Getsupportfragmentmanager (), r.id.realtabcontent);

Note that the r.id.realtabcontent here can be an object ID of either ViewGroup or its subclasses, such as LinearLayout. The actual function is a container, tab switch, the current tab corresponding to the fragment will be added to the viewgroup as its child view

5. Sequentially Add each tab page

[Java] view plaincopy

    1. Set the icon, text, and content for each tab button
    2. Tabspec Tabspec = Mtabhost.newtabspec ("TAG1"). Setindicator (Yourtabitemview);
    3. Add the tab button into the Tab tab
    4. Mtabhost.addtab (Tabspec, FragmentPage1. class, NULL);
    5. Set the background of the tab button
    6. Mtabhost.gettabwidget (). Getchildat (i). Setbackgroundresource (R.drawable.selector_tab_background);

Note that mtabhost.newtabspec ("TAG1"). Setindicator (Yourtabitemview); here "TAG1" doesn't really mean anything, just differentiate each tab.

The focus is on the Setindicator function, which has three different implementations, that is, you can use three ways to define your tab style:

[Java] view plaincopy

    1. Use only the tab that you want text to identify
    2. TabHost.TabSpec.setIndicator (Charsequence label)
    3. Use text +ICON to identify tab
    4. TabHost.TabSpec.setIndicator (charsequence label, drawable icon)
    5. Use a custom view to indicate tab
    6. TabHost.TabSpec.setIndicator (View view)

The first two tab styles are the styles we see in the vast majority of Tabhost (Holo style), which is a highlighted line (indicator) that resembles a scrollbar in the currently selected tab, and many times we don't need it, like the Style tab. At this point you can use the third way to customize your tab style, and you can implement any style of tab:

[Java] view plaincopy

    1. View Yourtabitemview = layoutinflater.inflate (R.layout.tab_item_view, NULL);
    2. ImageView ImageView = (ImageView) View.findviewbyid (R.id.imageview);
    3. Imageview.setimageresource (Mimageviewarray[index]);
    4. TextView TextView = (TextView) View.findviewbyid (R.id.textview);
    5. Textview.settext (Mtextviewarray[index]);

In addition, Fragmentpage1.class is a class that inherits from the fragment and is dynamically instantiated when the tab is toggled, and add to R.id.realtabcontent is displayed in the Content container.

Complete the above points, a simple fragementactivity+fragmenttabhost+fragment effect comes out, and then how to adjust tab docked at the top or bottom.

When R.id.realtabcontent and R.id.tabhost are not in a layout file, the default tab is tabcontent under, and tabcontent and tab cannot be adjusted.

When r.id.realtabcontent with R.id.tabhost in a layout file, if r.id.realtabcontent in R.id.tabhost above, then tab will be under Tabcontent, that is, the relative position of r.id.realtabcontent and R.id.tabhost determines whether the selected page on or under.

Do not set child view to Fragmenttabhost in the layout file, otherwise the child view will be displayed in the view with the top-left corner of the tab as the coordinate 0 point. Set android:layout_weight= "1" to r.id.realtabcontent because the default tabcontent height is wrap_content and cannot be adjusted when tab is in Tabcontent below and the displayed view is not enough to squeeze the tab to the bottom, tab will be hung at the end of the displayed view, after setting the tabcontent will be filled.

Tabhost Introduction to Android music idioms and the use of fragment replacement tabactivity

Related Article

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.