In Android, tabhost switches between different activities.

Source: Internet
Author: User

Tabhost is used in many applications. Sometimes setting a view in the tab added by tabhost cannot meet the requirements, because it is quite difficult to add preferenceactivity in the view.

Previously, you needed to use tabhost to switch different activities among multiple tabs in an application. One is the log list (listactivity), and the other is the application setting page (preferenceactivity)

First

Yeslog list page, yeslistactivity

Is set page, is a general preferenceactivity

Development Process:
1. Write different activities to switch when you click different tabs in tabhost (the following two examples are omitted because they are not the main content of this blog)
1-1. Write a logactivity, which is a listactivity and used to display the log Content.
1-2. Compile settingactivity, Which is preferenceactivity. The application setting page is written in the same way as preferenceactivity.

2. Compile the main page (mainactivity). This class is the entry class of the application. In this class, define the tabhost and add the corresponding activity.

// This class must inherit from activitygrouppublic class mainactivity extends activitygroup {private tabhost mtabhost; protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. requestwindowfeature (window. feature_no_title); setcontentview (R. layout. main); // set tabhost inittabs ();} private void inittabs () {mtabhost = (tabhost) findviewbyid (R. id. tabhost); mtabhost. setup (this. getlocalactivitymanager (); // Add the log List tab. Pay attention to the Code in the following setcontent. mtabhost is the key to achieving this requirement. addtab (mtabhost. newtabspec ("tab_log "). setindicator ("log", getresources (). getdrawable (R. drawable. ic_tab_log )). setcontent (new intent (this, logactivity. class); // Add the application settings tab. Pay attention to the Code in the following setcontent. mtabhost is the key to achieving this requirement. addtab (mtabhost. newtabspec ("tab_setting "). setindicator ("set", getresources (). getdrawable (R. drawable. ic_tab_settings )). setcontent (new intent (this, settingactivity. class); mtabhost. setcurrenttab (1 );}}

3. Compile the xml configuration file for layout of Main. xml and mainactivity.

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"xmlns:app="http://schemas.android.com/apk/res/rbase.app.nowscore"android:layout_height="fill_parent"                 android:layout_above="@+id/adLayout"><TabHost xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tabhost" android:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayout android:orientation="vertical"android:layout_width="fill_parent"                                 android:layout_height="wrap_content"><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" /></LinearLayout></TabHost></LinearLayout></RelativeLayout>

Supplement:
When you press the menu button in different tabs, the current activity prevails. That is, when you press the menu button in settingactivity, The settingactivity event is triggered, not the homepage event.

 

 

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.