Use of tabactivity in Android (1)

Source: Internet
Author: User

This article summarizes three basic tabactivity usage methods. The specific examples are taken from apidemo.

Basic knowledge

To implement a multi-tag activity, the target activity (the one you created) must first implement the tabactivity class. In this way, you can use the gettabhost () method to obtain the corresponding tabhost object and dynamically add tags. The following describes how to set the display area content during tag switching.

Create a display area layout from the layout File

The simplest way is to give the layout file and use layoutinflator to flate it to the display area. Since all labels are defined in the same layout file, framelayout is a required layout. Because you can only display one content at a time. In the first example of apidemo, the layout file is as follows:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView android:id="@+id/view1"        android:background="@drawable/blue"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="@string/tabs_1_tab_1"/>    <TextView android:id="@+id/view2"        android:background="@drawable/red"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="@string/tabs_1_tab_2"/>    <TextView android:id="@+id/view3"        android:background="@drawable/green"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="@string/tabs_1_tab_3"/>

Only one textview can be displayed at a time. In this way, we can implement it by providing its ID number in setcontent.

But first, we need to expand the layout to the display area. We can use tabhost. gettabcontentview () to obtain the reference of the view. The oncreate code of the activity is as follows:

        TabHost tabHost = getTabHost();                LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);        tabHost.addTab(tabHost.newTabSpec("tab1")                .setIndicator("tab1")                .setContent(R.id.view1));        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("tab2")                .setContent(R.id.view2));        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("tab3")                .setContent(R.id.view3));

The layoutinflator statement first constructs the layoutinflator instance from the current context, and then adds the layout file R. layout. tabs1 (as shown above) to the tabcontent.

Dynamically construct the display area content

The so-called dynamic structure means that when a tag is clicked, the specified interface is called and the view to be displayed is returned. This is obtained by implementing the tabhost. tabcontentfactory interface. The tabhost. tabcontentfactory interface provides the createtabcontent method. The procedure is as follows:

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        final TabHost tabHost = getTabHost();        tabHost.addTab(tabHost.newTabSpec("tab1")                .setIndicator("tab1", getResources().getDrawable(R.drawable.star_big_on))                .setContent(this));        tabHost.addTab(tabHost.newTabSpec("tab2")                .setIndicator("tab2")                .setContent(this));        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("tab3")                .setContent(this));    }    /** {@inheritDoc} */    public View createTabContent(String tag) {        final TextView tv = new TextView(this);        tv.setText("Content for tab with tag " + tag);        return tv;    }

Because setcontext is set to this, createtabcontent is called to construct the display area content. The tag passed in by this function is the tag defined in newtabspec. Createtabcontent determines which tag is clicked Based on the tag, and then dynamically constructs the view and returns it.

Use intent to call other activities

Tabactivity is most commonly used to combine multiple different activities in one interface through tags. This is implemented through intent. You can pass the intent instance to setcontent. Example:

    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        final TabHost tabHost = getTabHost();        tabHost.addTab(tabHost.newTabSpec("tab1")                .setIndicator("list")                .setContent(new Intent(this, List1.class)));        tabHost.addTab(tabHost.newTabSpec("tab2")                .setIndicator("photo list")                .setContent(new Intent(this, List8.class)));                // This tab sets the intent flag so that it is recreated each time        // the tab is clicked.        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("destroy")                .setContent(new Intent(this, Controls2.class)                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));    }

Apidemo's view-> tabs provides three specific instances, which are very comprehensive.

Conclusion

Tabactivity customization is very important and commonly used, but its basic use has been able to meet the needs of the vast majority. It should be said that this is a must for Android development.

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.