Android Custom Tabhost,tabcontent for intent

Source: Internet
Author: User

Address: http://my.oschina.net/aowu/blog/36282

Customize the Tabhost build yourself, such as left. Have better friends can communicate with each other, hey.

1. First up Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.priscilla"
Android:versioncode= "1"
Android:versionname= "1.0" >

<USES-SDK android:minsdkversion= "7"/>

    <application
        android:icon= "@drawable/icon "
        android:label=" @string/app_name ";
         <activity
            Android:label= "@string/app_name"
            android: Name= ". Mytab,
            <intent-filter;
                <action android: Name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name= ". Tabcontent ">
</activity>
</application>

</manifest>

2 Mytab.java

Package Com.priscilla;

Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Map;
Import Java.util.WeakHashMap;

Import android.app.TabActivity;
Import Android.content.Context;
Import android.content.Intent;
Import android.content.res.Resources;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.LinearLayout;
Import Android.widget.TabHost;
Import Android.widget.TabWidget;
Import Android.widget.TextView;
Import Android.widget.TabHost.TabSpec;

public class Mytab extends Tabactivity {
/** called when the activity is first created. */

Private Tabhost Mtabhost;
Private Tabwidget Mtabwidget;
Private Layoutinflater Minflater;
Private list<textview> Mtext;
Private list<tabspec> Mtabspec;
Private list<linearlayout> LinearLayout;
Private list<intent> Intent;
Private Context Mcontext;

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Mcontext = this;
Minflater = Layoutinflater.from (this);

Mtabhost = This.gettabhost ();//Get tab Tabhost from tabactivity
Mtabhost = (tabhost) Findviewbyid (Android. R.id.tabhost);
Mtabwidget = (tabwidget) Findviewbyid (Android. R.id.tabs);

Mtabspec = new arraylist<tabspec> ();
LinearLayout = new arraylist<linearlayout> ();
Mtext = new arraylist<textview> ();
Intent = new arraylist<intent> ();

Creattab ();

/*
* Mtabhost.addtab (mtabhost. Newtabspec ("tab 1"). Setindicator (
* (LinearLayout) Layoutinflater.from (this). Inflate (R.layout.tabwidget,
* null)). SetContent (r.id.linearlayout01)); Mtabhost.addtab (mtabhost
*. NEWTABSPEC ("tab 2"). Setindicator ((linearlayout)
* Layoutinflater.from (this). Inflate (R.layout.tabwidget, NULL))
*. setcontent (r.id.linearlayout02)); Mtabhost.addtab (mtabhost
*. NEWTABSPEC ("tab 3"). Setindicator ((linearlayout)
* Layoutinflater.from (this). Inflate (R.layout.tabwidget, NULL))
*. setcontent (r.id.linearlayout03));
*/

Sets which label is currently displayed
Mtabhost.setcurrenttab (0);

Go to tabwidget white line, this is valid for the default theme
/*
* Mtabhost.setpadding (Mtabhost.getpaddingleft (),
* Mtabhost.getpaddingtop (), Mtabhost.getpaddingright (),
* Mtabhost.getpaddingbottom ()-5);
*/
}

public void Creattab () {

for (int i = 0; i < 3; i++) {
Mtabspec.add (Mtabhost.newtabspec ("tab" + i));
Linearlayout.add ((linearlayout) minflater.inflate (
R.layout.tabwidget, null));
Mtext.add ((TextView) linearlayout.get (i)
. Findviewbyid (R.id.tab_name));
Mtext.get (i). SetText (
Mcontext.getresources (). getString (R.string.tab_name,
String.valueof (i)));
Mtabspec.get (i). Setindicator (Linearlayout.get (i));
Mtabspec.get (i). SetContent (List.get (i));
Intent.add (New Intent (Mcontext, Tabcontent.class));
LOG.V ("---whty---", mtabspec.get (i). Gettag ());
Bundle Buddle = new bundle ();
Buddle.putstring ("tab", Mtabspec.get (i). Gettag ());
Intent.get (i). Putextras (Buddle);

Mtabspec.get (i). SetContent (Intent.get (i));

Mtabhost.addtab (Mtabspec.get (i));

}
}

}

2.tabcontent.java

Package Com.priscilla;

Import android.app.Activity;
Import android.content.Intent;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.os.Bundle;
Import Android.widget.ImageView;
Import Android.widget.TextView;

public class Tabcontent extends Activity {

Private TextView TextView;
Private ImageView Imgview;

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.tabcontent);

TextView = (TextView) Findviewbyid (R.id.textview);
Imgview = (ImageView) Findviewbyid (R.id.imageview);

Bundle Buddle = This.getintent (). Getextras ();
String flag = buddle.getstring ("tab");

if ("Tab0". Equals (flag)) {
Textview.settext (Getresources (). getString (R.string.andy));
Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (),
R.drawable.andy);
Imgview.setimagebitmap (bitmap);
}

if ("Tab1". Equals (flag)) {
Textview.settext (Getresources (). getString (R.string.bill));
Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (),
R.drawable.bill);
Imgview.setimagebitmap (bitmap);
}

if ("TaB2". Equals (flag)) {
Textview.settext (Getresources (). getString (R.string.linux));
Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (),
R.drawable.torvalds);
Imgview.setimagebitmap (bitmap);
}

}

}

3.3 Layout

Main.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<tabhost xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@android: Id/tabhost"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:background= "@color/WCITY_NORMAL_BG" >

<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"
android:background= "@drawable/con_film_bottom_tab"/>

<framelayout
Android:id= "@android: Id/tabcontent"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
</FrameLayout>
</LinearLayout>

</TabHost>

Tabwidget.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Horizontal"
android:gravity= "Center" >
<textview
Android:id= "@+id/tab_name"
Android:layout_width= "Fill_parent"
android:layout_height= "39DP"
android:layout_marginleft= "2DP"
android:layout_marginright= "2DP"
Android:ellipsize= "Marquee"
android:marqueerepeatlimit= "Marquee_forever"
Android:singleline= "true"
android:gravity= "Center"
Android:textcolor= "@drawable/tab_selector"
android:background= "@drawable/tab_bg_selector"/>
</LinearLayout>

Tabcontent.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/linearlayout"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:gravity= "Center_horizontal"
android:orientation= "Vertical" >

<imageview
Android:id= "@+id/imageview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
Android:scaletype= "Fitxy"/>

<textview
Android:id= "@+id/textview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "24dip"/>

</LinearLayout>

4.2 x drawable

Tab_bg_selector.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_selected= "true" android:drawable= "@drawable/con_film_tab_pressed"/>
</selector>

Tab_selector.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_selected= "false" android:color= "#ffffffff"/>
<item android:state_selected= "true" android:color= "#ffEEC900"/>
</selector>

6.colors.xml String.xml

Colors.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<color name= "WCITY_NORMAL_BG" > #ffe5eff4 </color>
</resources>

String.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "Hello" >hello World, my</string>
<string name= "App_name" >myTab</string>
<string name= the creator of "Andy" >android: Andy Rubin</string>
<string Name= One of the >java creators of "Bill": Bill Joy</string>
<string name= "Linux" >linux's father: Linus torvalds</string>
<string name= "Tab_name" > Tabs%1$s</string>
</resources>

Android Custom Tabhost,tabcontent for intent

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.