Detailed Android Tabhost A variety of implementation methods attached source download _android

Source: Internet
Author: User

Recently carefully studied the next tabhost, mainly in order to achieve the micro-letter bottom navigation bar function, and finally give a link to the article, we do not worry

Body:

The realization of tabhost is divided into two kinds, one is not inheriting tabactivity, the other is inheriting from tabactivity; Of course, it is relatively easy to choose the inheritance from Tabactivity, Let's take a look at how the difference is achieved.

method One, define Tabhost: Do not inherit tabactivity
1. layout file: Activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:o" rientation= "Vertical" tools:context= ". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "android:layout_height=" "Wrap_content" android:text= button "/> <tabhost android:id=" @+id/tabhost "android:layout_width=" Match_paren T "android:layout_height=" wrap_content "> <linearlayout android:layout_width=" match_parent "android:layou t_height= "match_parent" android:orientation= "vertical" > <tabwidget android:id= "@android: Id/tabs" Andr 
  Oid:layout_width= "Match_parent" android:layout_height= "wrap_content" > </TabWidget> <framelayout 
 
  Android:id= "@android: Id/tabcontent" android:layout_width= "match_parent" android:layout_height= "Match_parent" > <!--Firsttab layout--> <linearlayout android:id= "@+id/tab1" android:layout_width= "Match_parent" Android:layout_h eight= "Match_parent" > <textview android:id= "@+id/textview1" android:layout_width= "Wrap_content" a 
  ndroid:layout_height= "Wrap_content" android:text= "Lin Bingdong"/> </LinearLayout> <!--the second tab layout--> <linearlayout android:id= "@+id/tab2" android:layout_width= match_parent "android:layout_height=" Match_pa Rent "> <textview android:id=" @+id/textview2 "android:layout_width=" Wrap_content "Android:layout_h" eight= "Wrap_content" android:text= "Zhangxiaoyuan"/> </LinearLayout> <!--The layout of the Third tab--> 
 
   Yout android:id= "@+id/tab3" android:layout_width= "match_parent" android:layout_height= "Match_parent" > <textview android:id= "@+id/textview3" android:layout_width= wrap_content "android:layout_height=" Wrap_con Tent "android:text="Mabebe "/> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </linearlay 
 Out>

2, Java code

public class Mainactivity extends activity { 
 
 @Override 
 protected void onCreate (Bundle savedinstancestate) { 
 super.oncreate (savedinstancestate); 
 Setcontentview (r.layout.activity_main); 
  
 Tabhost th= (tabhost) Findviewbyid (r.id.tabhost); 
 Th.setup ();  Initialize Tabhost container 
  
 //Create label in Tabhost, then set: Title/Icon/tab Layout 
 Th.addtab (Th.newtabspec ("Tab1"). Setindicator ("Tag 1", Getresources (). getdrawable (R.drawable.ic_launcher)). SetContent (R.ID.TAB1)); 
 Th.addtab (Th.newtabspec ("TaB2"). Setindicator ("label 2", null). SetContent (R.ID.TAB2)); 
 Th.addtab (Th.newtabspec ("Tab3"). Setindicator ("label 3", null). SetContent (R.ID.TAB3)); 
 
 The above null can be Getresources (). getdrawable (r.drawable. Picture name) setting icon 
 
 } 
 

Effect Chart:

This example source address: Demo1

Method Two,the content of the tab separate: Do not inherit tabactivity
1, the first tab of the XML layout file, Tab1.xml:

<?xml version= "1.0" encoding= "UTF-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
 android:id=" @+id/linearlayout01 " 
 android:layout_width=" wrap_content " 
 android:layout_" height= "Wrap_content" > 
 <textview 
  android:text= "I'm the content of tag 1." 
  android:id= "@+id/textview01" 
  Android:layout_width= "Wrap_content" 
  android:layout_height= "wrap_content" > 
 </TextView> 
 </LinearLayout> 

2, the Second tab of the XML layout file, Tab2.xml:

<?xml version= "1.0" encoding= "UTF-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
 android:id=" @+id/linearlayout02 " 
 android:layout_width=" wrap_content " 
 android:layout_" height= "Wrap_content" > 
 
 <textview android:text= "Tag 2" 
   android:id= "@+id/textview01" 
   android: Layout_width= "Wrap_content" 
   android:layout_height= "wrap_content"/> 
</LinearLayout> 

3, the main layout file, Activity_main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" 
 android:layout_width= "Fill_ Parent " 
 android:layout_height=" fill_parent " 
 android:orientation=" vertical "> 
 
 <tabhost 
 Android:id= "@+id/tabhost"  
 android:layout_width= "match_parent" 
 android:layout_height= "Match_parent" > 
 
 <linearlayout 
  android:layout_width= "match_parent" 
  android:layout_height= "Match_parent" 
  android:orientation= "vertical" > 
 
  <tabwidget 
  android:id= "@android: Id/tabs" 
  android:layout_ Width= "Match_parent" 
  android:layout_height= "wrap_content" > 
  </TabWidget> 
 
  < Framelayout 
  android:id= "@android: id/tabcontent" 
  android:layout_width= "Match_parent" 
  android: layout_height= "Match_parent" > 
   
  </FrameLayout> 
 </LinearLayout> 
 </TabHost> 
 
</LinearLayout> 

4, Java code:

public class Mainactivity extends activity { 
 
 @Override 
 protected void onCreate (Bundle savedinstancestate) { 
 super.oncreate (savedinstancestate); 
 Setcontentview (r.layout.activity_main); 
  
 Tabhost m = (tabhost) Findviewbyid (r.id.tabhost); 
 M.setup (); 
  
 Layoutinflater I=layoutinflater.from (this); 
 I.inflate (R.LAYOUT.TAB1, M.gettabcontentview ()); 
 I.inflate (R.LAYOUT.TAB2, M.gettabcontentview ());//dynamically load XML without the need for activity 
  
 M.addtab (M.newtabspec ("Tab1"). Setindicator ("label 1"). SetContent (r.id.linearlayout01)); 
  M.addtab (M.newtabspec ("TaB2"). Setindicator ("Label 2"). SetContent (r.id.linearlayout02)); 
 

Effect Chart:


This example source address: Demo2

methods Three, inherit from tabactivity
1, the main layout file, Activity_main.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <framelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" Fill_parent "> <!--first layout--> <lin Earlayout android:id= "@+id/view1" android:layout_width= "match_parent" android:layout_height= "match_parent" > &L T 
  TextView android:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Zhangxiaoyuan"/> </LinearLayout> <!--the second layout--> <linearlayout android:id= "@+id/view2" a Ndroid:layout_width= "Match_parent" android:layout_height= "match_parent" > <textview android:id= "@+id/textV" Iew2 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" Horse Beibei "/> </Li Nearlayout> <!--third layout--> <textview android:id= "@+id/view3" android:background= "#00ff00" Android:la Yout_width= "Fill_parent" Android:layout_height= "Fill_parent" android:text= "Tab3"/> </FrameLayout> 
 

2, Java code:
First change the derived from activity to tabactivity, and then the code is as follows:  

public class Mainactivity extends tabactivity {@Override protected void onCreate (Bundle savedinstancestate) {su 
 Per.oncreate (savedinstancestate); 
 Settitle ("tabdemoactivity"); 
 Tabhost tabhost = Gettabhost (); 
 Layoutinflater.from (This). Inflate (R.layout.activity_main, Tabhost.gettabcontentview (), true); Tabhost.addtab (Tabhost.newtabspec ("Tab1"). Setindicator ("Tab1", Getresources (). Getdrawable (R.drawable.ic_ 
 Launcher)). 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)); Label Toggle event Handling, Setontabchangedlistener Tabhost.setontabchangedlistener (new Ontabchangelistener () {@Override public voi 
  D ontabchanged (String tabid) {if (Tabid.equals ("Tab1")) {//First label} if (Tabid.equals ("TaB2")) {//Second label} 
  
  
 if (Tabid.equals ("Tab3")) {//Third Tab}}); 
 } 
 
}

Effect Chart:

This example source address: Demo3

Implementation of micro-letter bottom navigation bar

Effect:

refer to: Android Imitation micro-letter Bottom menu bar function display unread message quantity

Original address: http://blog.csdn.net/harvic880925/article/details/17120325

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.