Andorid tabhost usage Summary

Source: Internet
Author: User

The tabhost control in Android is a very useful control. tabhost can be used for pages displayed by modules.

Tabhost is a set of tabs composed of tabspac. Tabspec mainly has two important methods:

    /**     * A tab has a tab indicator, content, and a tag that is used to keep     * track of it.  This builder helps choose among these options.     *     * For the tab indicator, your choices are:     * 1) set a label     * 2) set a label and an icon     *     * For the tab content, your choices are:     * 1) the id of a {@link View}     * 2) a {@link TabContentFactory} that creates the {@link View} content.     * 3) an {@link Intent} that launches an {@link android.app.Activity}.     */    public class TabSpec {        private String mTag;        private IndicatorStrategy mIndicatorStrategy;        private ContentStrategy mContentStrategy;        private TabSpec(String tag) {            mTag = tag;        }        /**         * Specify a label as the tab indicator.         */        public TabSpec setIndicator(CharSequence label) {            mIndicatorStrategy = new LabelIndicatorStrategy(label);            return this;        }        /**         * Specify a label and icon as the tab indicator.         */        public TabSpec setIndicator(CharSequence label, Drawable icon) {            mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon);            return this;        }        /**         * Specify a view as the tab indicator.         */        public TabSpec setIndicator(View view) {            mIndicatorStrategy = new ViewIndicatorStrategy(view);            return this;        }        /**         * Specify the id of the view that should be used as the content         * of the tab.         */        public TabSpec setContent(int viewId) {            mContentStrategy = new ViewIdContentStrategy(viewId);            return this;        }        /**         * Specify a {@link android.widget.TabHost.TabContentFactory} to use to         * create the content of the tab.         */        public TabSpec setContent(TabContentFactory contentFactory) {            mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);            return this;        }        /**         * Specify an intent to use to launch an activity as the tab content.         */        public TabSpec setContent(Intent intent) {            mContentStrategy = new IntentContentStrategy(mTag, intent);            return this;        }

Setindicator () allows you to set tab icons and text.

Note: 1. If your tabhost is obtained from the findviewbyid () in the XML file,

Tabwidget must be Android: Id = "@ Android: ID/tabs ",

Framelayout Android: Id = "@ Android: ID/tabcontent ";

<TabHost android:id="@+id/tabhost_info" android:layout_width="fill_parent"android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><LinearLayout android:id="@+id/linearLayout"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical"><TabWidget android:id="@android:id/tabs"android:layout_width="fill_parent" android:layout_height="wrap_content"></TabWidget><FrameLayout android:id="@android:id/tabcontent"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_gravity="fill"><include android:id="@+id/info_include01" layout="@layout/info_layout01" /><include android:id="@+id/info_include02" layout="@layout/info_layout02" /><include android:id="@+id/info_include03" layout="@layout/info_layout03" /></FrameLayout></LinearLayout></TabHost>

2. In the code, call the setup () method before adding a tabwidget.

Tabhost = (tabhost) findviewbyid (R. id. tabhost_info); tabhost. setup (); tabhost. addtab (tabhost. newtabspec ("info "). setcontent (r.id.info _ include01 ). setindicator ("basic information", getresources (). getdrawable (R. drawable. ic_launcher); tabhost. addtab (tabhost. newtabspec ("More information "). setcontent (r.id.info _ include02 ). setindicator ("More information", getresources (). getdrawable (R. drawable. ic_launcher); tabhost. addtab (tabhost. newtabspec ("attachment download "). setcontent (r.id.info _ include03 ). setindicator ("attachment download", getresources (). getdrawable (R. drawable. ic_launcher )));

The following is a self-written Demo:


Demo: http://download.csdn.net/detail/china1988s/4072957

 

References: http://mmqzlj.blog.51cto.com/2092359/642465

 

 

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.