Android Development Step by Step 31:tabactivity usage tab appears at the bottom

Source: Internet
Author: User

Tab tab is a very common control, in. NET there is a multipage+tabstrip combination through the switch of the tag to implement the page switch, the same HTML we commonly used frameset to achieve, Android inside we use Framelayout+tabwidget, framelayout inside Load XML page, tabwidget display tag, click Tag to jump to related activity or view.
The detailed inheritance relationship of the public class Tabactivity extends Activitygroup is as follows:
Java.lang.Object
? Android.content.Context
? Android.content.ContextWrapper
? Android.view.ContextThemeWrapper
? Android.app.Activity
? Android.app.ActivityGroup
? Android.app.TabActivity
As you can see, tabactivity is inherited from activity and contains a tabhost component. The Tabhost component is a viewgroup that inherits from Framelayout. The ID of the Tabhost component itself must be @android:id/tabhost, it must contain a framelayout, and the Framelayout ID must be @android:id/tabcontent, Also include a tabwidget,id that is @android:id/tabs. Framelayout can place each individual activity, while Tabwidget is a tab check. The activity that corresponds to the default first tab is displayed first in Framelayout. Then each time you click on another tab, the activity will switch to the framelayout. This is somewhat similar to the concept of frameset in HTML.
Good, the theory of knowledge on the introduction of so much, to begin our practice, we want to achieve the effect is: the bottom of the page to put a tabwidget, the page header display a logo and text introduction, the page body shows the specific activity corresponding page. Make tab display at the bottom of the settings is simple, as long as the Tabwidget property android:layout_alignparentbottom= "true"
Android:layout_alignparenttop= "false"
This can be set, to be displayed at the top of the reverse can be
Android:layout_alignparentbottom= "false"
Android:layout_alignparenttop= "true"

First step: Create a page
Main Page: Tabactivityview.xml contains header head_line page body framelayout footer Tabwidget
<?xml version= "1.0" encoding= "UTF-8"?>
<tabhost android:id= "@android: Id/tabhost" android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" xmlns:android= "http://schemas.android.com/apk/res/android" >

<relativelayout android:orientation= "Vertical"
Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" >

<include android:id= "@+id/head_line" layout= "@layout/head_line"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"/>

<framelayout android:id= "@android: Id/tabcontent"
android:layout_below= "@id/head_line" android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" android:layout_weight= "1.0"/>

<tabwidget android:id= "@android: Id/tabs"
android:layout_gravity= "Bottom" android:layout_height= "60.0dip"
Android:layout_width= "Fill_parent" android:fadingedge= "None"
Android:fadingedgelength= "0.0px" android:paddingleft= "0.0dip"
android:paddingtop= "2.0dip" android:paddingright= "0.0dip"
Android:paddingbottom= "0.0dip" android:layout_alignparentbottom= "false"
Android:layout_alignparenttop= "true"/>

</RelativeLayout>

</TabHost>

Page Header: Head_line.xml contains icons and text

<relativelayout
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
<imageview android:layout_height= "wrap_content" android:src= "@drawable/icon" android:layout_width= "Wrap_ Content "android:id=" @+id/imageview1 "android:layout_alignparenttop=" true "android:layout_alignleft=" @+id/top_btn _left "></ImageView>
<textview android:gravity= "Center_horizontal" android:text= "Better Pay,better Life" android:layout_height= "wrap_ Content "Android:textsize=" 22.0sp "android:layout_width=" Wrap_content "android:layout_alignwithparentifmissing=" True "android:singleline=" true "android:id=" @+id/top_title "android:ellipsize=" Middle "android:layout_ Centervertical= "true" android:layout_centerhorizontal= "true" ></TextView>

</RelativeLayout>

Tags: tab_item.xml contains icons and text
<?xml version= "1.0" encoding= "UTF-8"?>
<linearlayout android:orientation= "Vertical"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
<imageview android:id= "@+id/tab_item_imageview"
Android:layout_width= "Fill_parent" android:layout_height= "32.0dip"
Android:scaletype= "Fitcenter"/>
<textview android:id= "@+id/tab_item_textview"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
android:gravity= "Center" android:singleline= "true"
android:marqueerepeatlimit= "1" android:textsize= "11.0SP"
Android:ellipsize= "Marquee"/>
</LinearLayout>

Step two, create activity Mytabactivity.java
/**
*
*/
Package Com.figo.helloworld;

Import android.app.TabActivity;
Import android.content.Intent;
Import android.content.res.Resources;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ImageView;
Import Android.widget.TabHost;
Import Android.widget.TabHost.TabSpec;
Import Android.widget.TextView;

/**
* @author Zhuzhifei
*
*/
public class Mytabactivity extends Tabactivity {

Private Tabhost Tabhost;

@Override
public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);
Setcontentview (R.layout.tabactivityview);//Set the displayed page
Tabhost = Gettabhost ();//Get Tabhost
Createtab ();//Create Tab Collection

}

/**
* Assemble tab control
*/
private void Createtab () {

Resources res = getresources ();
Createtabitem (R.drawable.icon,
Res.getstring (R.string.pay), new Intent (This,
Helloworldactivity.class));
Createtabitem (R.drawable.icon,
Res.getstring (r.string.transact), new Intent (This,
Listviewactivity.class));
Createtabitem (R.drawable.icon,
Res.getstring (R.string.account), new Intent (This,
Helloworldactivity.class));
Createtabitem (R.drawable.icon,
Res.getstring (R.string.message), new Intent (This,
Helloworldactivity.class));
Createtabitem (R.drawable.icon,
Res.getstring (R.string.tip), new Intent (This,
Helloworldactivity.class));

}

/**
* Generate Tab_item
*
* @param imageresourceselector
* Image Selector
* @param text
* Text
* @param Intent
* Intent
*/
private void Createtabitem (int imageresourceselector, String text,
Intent Intent) {

View view = View.inflate (this, r.layout.tab_item, null);//Assemble view
((ImageView) View.findviewbyid (R.id.tab_item_imageview))
. Setimageresource (Imageresourceselector);
((TextView) View.findviewbyid (R.id.tab_item_textview)). SetText (text);

Tabspec spec = tabhost.newtabspec (text). Setindicator (view)
. SetContent (intent);//view injection spec,setcontent has multiple overloaded methods for use
Tabhost.addtab (spec);

}
}


The third step, Androidmanifest.xml registered activity
<activity android:name= ". Mytabactivity ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Fourth step, Operation effect
Select the first tab


Select a second tab

Android Development Step by Step 31:tabactivity usage tab appears at the bottom

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.