Android components must learn the tabhost use of the method detailed _android

Source: Internet
Author: User
Tags tagname visibility

First, tabhost usage
Typically, we will invoke Gettabhost () to get tabhost instances by inheriting tabactivity, which is the specific procedure below.
Tabhostactivity.java

public class Tabhostactivity extends Tabactivity {private Tabhost tabhost;
 Private Intent certificateintent;
 Private Intent feeintent;
 Private Intent scoreintent;
 Private Intent studyintent;
 
 Private Intent moreintent;
  @Override publicvoid onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Tabhost = Gettabhost ();
  Initintent ();
 Addspec ();
  /** * initializes each tab label corresponding to the Intent/privatevoid initintent () {studyintent = new Intent (this, studyactivity.class);
  Scoreintent = new Intent (this, scoreactivity.class);
  Feeintent = new Intent (this, feeactivity.class);
  Certificateintent = new Intent (this, certificateactivity.class);
 Moreintent = new Intent (this, moreactivity.class); /** * Add individual label items to Tabhost * * privatevoid Addspec () {Tabhost.addtab this.buildtagspec ("Tab_study", R.STRING.STUDY_PR
 OGRESS,R.DRAWABLE.ACCOUNT01, studyintent));
 Tabhost.addtab (This.buildtagspec ("Tab_score", r.string.test_score,r.drawable.account02, scoreIntent)); Tabhost.addtab (This.buildtagspec ("Tab_fee", r.string.fee_pay,r.drawable.account03, feeIntent)); Tabhost.addtab (This.buildtagspec ("Tab_certificate", R.string.certificate_grant,
  R.drawable.accountcertificateintent));
 Tabhost.addtab (This.buildtagspec ("Tab_more", R.string.more, r.drawable.account05, moreintent));
  /** * Customize the method of creating label items * @param tagName tag ID * @param taglable label text * @param icon label icon * @param content Label * @return * * Private Tabhost.tabspec Buildtagspec (String tagName, int taglable, int icon, Intent content) {return Tabhost. Newtabspec (TagName). Setindicator (Getresources (). getString (taglable), Getresources (). getdrawable (IC
 ON). SetContent (content);
 }}

The results of the operation are shown in the following figure

We found that the label position is above the interface, but many of the apps we see are labeled at the bottom of the interface.
As shown in the following figure

We want to achieve this effect by simply overwriting the tabactivity default layout. The new layout only needs to change the label and the relative position of the corresponding content of the label, and here is the position of the label corresponding to the top of the label with relative layout. Do not change the ID (throws an exception, the hint must use the specified ID). Don't forget to set a new layout in OnCreate () to overwrite the tabactivity default layout.

 @Override
 publicvoid onCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
Setcontentview (r.layout.tab);
  Tabhost = Gettabhost ();
  Initintent ();
  Addspec ();
 }

Tab.xml

 <?xml version= "1.0" encoding= "UTF-8"?> <!--tabhost Component ID value immutable--> < Tabhostxmlns:android=http://schemas.android.com/apk/res/android android:id= "@android: Id/tabhost" Android:layout_
  height= "Fill_parent" android:layout_width= "fill_parent" > <relativelayout android:orientation= "Vertical" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" > <!--tabwidget Component ID value immutable--> Bwidget android:id= "@android: Id/tabs" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" and Roid:layout_alignparentbottom= "true" > </TabWidget> <!--framelayout layout, id value immutable--> <framelayout an Droid:id= "@android: Id/tabcontent" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Android: Layout_above= "@android: Id/tabs" > </FrameLayout> </RelativeLayout> </TabHost> 

Usually we have a custom activity base class in the project, and we let all the interface activity inherit the base class. But to use Tabhost will inherit tabactivity, so we can define two base classes, one is the base class of the ordinary activity interface, the other is the base class that contains the Tabhost interface, let this base class inherit tabactivity.
Second, Tabhost Usage-Define TAB style
in the first section, "Tabhost usage," we introduce the effect of a tab page through Tabhost . But in the actual project we might prefer to define our own tab style to make the interface better. Since you can't change the tab style of the system, we can choose to hide the system and use what we define (this is a good way to go in detail later). Anti-compiling Sina Weibo project will find that they hide the Tabwidget tab tag in the layout and use a set of RadioButton instead. Since it is defined by itself, it must be possible to decide on their own display style, then our problem will be solved.

Here I use the second implementation mentioned in the "Tabhost usage-two implementations" article, which inherits the activity to use the tabhost. Post the Code first (the red font is part of the code you modified or added).
Tabhostactivity.java

public class Tabhostactivity extends activity implements Oncheckedchangelistener {private Tabhost tabhost;
 Private Intent certificateintent;
 Private Intent feeintent;
 Private Intent scoreintent;
 Private Intent studyintent;
 
 Private Intent moreintent;
  @Override publicvoid onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.tab);
  Tabhost = Gettabhost ();
  Tabhost = (tabhost) Findviewbyid (r.id.my_tabhost);
  Localactivitymanager groupactivity = new Localactivitymanager (this,false);
  Groupactivity.dispatchcreate (savedinstancestate);
  Tabhost.setup (groupactivity);
  Initintent ();
  Addspec ();
 
 ((Radiogroup) Findviewbyid (R.id.tab_radiogroup)). Setoncheckedchangelistener (this);
  /** * initializes each tab label corresponding to the Intent/privatevoid initintent () {studyintent = new Intent (this, studyactivity.class);
  Scoreintent = new Intent (this, scoreactivity.class);
  Feeintent = new Intent (this, feeactivity.class); CertificatEintent = new Intent (this, certificateactivity.class);
 Moreintent = new Intent (this, moreactivity.class); /** * Add individual label items to Tabhost * * privatevoid Addspec () {Tabhost.addtab this.buildtagspec ("Tab_study", R.STRING.STUDY_PR
Ogress, r.drawable.account01, studyintent));
  Tabhost.addtab (This.buildtagspec ("Tab_score", R.string.test_score, r.drawable.account02, scoreIntent));
  Tabhost.addtab (This.buildtagspec ("Tab_fee", R.string.fee_pay, r.drawable.account03, feeintent)); Tabhost.addtab (This.buildtagspec) ("Tab_certificate", R.string.certificate_grant, r.drawable.account04, certificate
  Intent));
 Tabhost.addtab (This.buildtagspec ("Tab_more", R.string.more, r.drawable.account05, moreintent));
  /** * Customize the method of creating label items * @param tagName tag ID * @param taglable label text * @param icon label icon * @param content Label * @return * * Private Tabhost.tabspec Buildtagspec (String tagName, int taglable, int icon, Intent content) {Retu Rntabhost. NEWTABSPEC (TagName). Setindicator (Getresources (). getString (taglable), Getresources (). getdrawable (Icon)). setcontent (content); @Override public void OnCheckedChanged (radiogroup group, int checkedid) {switch (checkedid) {case R.id.radio_
   Button_study:tabHost.setCurrentTabByTag ("Tab_study");
  Break
   Case R.id.radio_button_score:tabhost.setcurrenttabbytag ("Tab_score");
  Break
   Case R.id.radio_button_certificate:tabhost.setcurrenttabbytag ("tab_certificate");
  Break
   Case R.id.radio_button_fee:tabhost.setcurrenttabbytag ("Tab_fee");
  Break
   Case R.id.radio_button_more:tabhost.setcurrenttabbytag ("Tab_more");
  Break

 }
 
 }
}

Tab.xml

<?xml version= "1.0" encoding= "UTF-8"?> <tabhost android:id= "@+id/my_tabhost" android:layout_width= "Fill_" Parent "android:layout_height=" fill_parent "xmlns:android=" http://schemas.android.com/apk/res/android "> < LinearLayout android:orientation= "vertical" android:layout_width= fill_parent "android:layout_height=" fill_parent "> <framelayout android:id=" @android: Id/tabcontent "android:layout_width=" Fill_parent "Android:layout_height" = "0.0dip" android:layout_weight= "1.0"/> <tabwidget android:id= "@android: Id/tabs" android:visibility= "Gone" an Droid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_weight= "0.0"/> <RadioG Roup android:id= "@+id/tab_radiogroup" android:background= "@drawable/tabs_bg" android:layout_width= "Fill_parent" an droid:layout_height= "Wrap_content" android:gravity= "center_vertical" android:layout_gravity= "Bottom" android:o rientation= "Horizontal" > <radiobutton android:id= "@+id/radio_button_study "android:layout_margintop=" 2.0dip "android:text=" Learning Progress "android:drawabletop=" @drawable/acco unt01 "style=" @style/tab_button_bottom "android:checked=" true "/> <radiobutton" android:id= Score "android:layout_margintop=" 2.0dip "android:text=" Test Results "android:drawabletop=" @drawable/account02 "style=" @sty Le/tab_button_bottom "/> <radiobutton android:id=" @+id/radio_button_certificate "android:layout_marginTop=" 2 .0dip "android:text=" certificate Issued "android:drawabletop=" @drawable/account03 "style=" @style/tab_button_bottom "/> <rad Iobutton android:id= "@+id/radio_button_fee" android:layout_margintop= "2.0dip" android:text= "Fee Payment" android:drawable top= "@drawable/account04" style= "@style/tab_button_bottom"/> <radiobutton android:id= "@+id/radio_button_ More "android:layout_margintop=" 2.0dip "android:text=" more "android:drawabletop=" @drawable/account05 "style=" @style Tab_button_bottom "/> </raDiogroup> </LinearLayout> </TabHost>
 

Styles.xml

<?xml version= "1.0" encoding= "Utf-8"?> <resources> <!--tabhost label button style--> <style name= "Tab_button _bottom "> <item name=" android:textsize ">12px</item> <item name=" Android:textcolor "> #ffffffff </item> <item name= "android:ellipsize" >marquee</item> <item name= "Android:gravity" >center_ horizontal</item> <item name= "Android:background" > @drawable/tab_btn_bg</item> <item " Android:layout_margintop ">2.0dip</item> <item name=" Android:button "> @null </item> <item Name= "Android:paddingtop" >6dip</item> <item name= "android:drawablepadding" >4dip</item> < Item Name= "Android:layout_width" >fill_parent</item> <item name= "Android:layout_height" >wrap_ content</item> <item name= "android:layout_weight" >1.0</item> <item name= "Android:singleline" >true</item> </style> <!--page title LinearLayout style--&GT <style name= "Activity_title_background" > <item name= "android:background" > @drawable/title_background </item> <item name= "android:layout_width" >fill_parent</item> <item name= "Android:layout_" Height ">wrap_content</item> <item name=" Android:layout_alignparenttop ">true</item> <item Name= "Android:gravity" >center</item> </style> <!--interface title TextView style--> <style name= "Activity_ Title_text "> <item name=" android:textsize ">14dip</item> <item name=" Android:textcolor ">@ drawable/white</item> <item name= "android:layout_width" >wrap_content</item> <item name= " Android:layout_height ">wrap_content</item> <item name=" android:gravity ">center</item> </
 Style> </resources>

The results of the operation are shown in the following figure


Important parts of the program:
1. The Red font section.
2. Tab.xml The layout file, you can see that the layout file hides Tabwidget (android:visibility= "Gone") and replaces it with a radiogroup.
3. For Radiogroup set Oncheckedchangelistener monitoring, through the OnCheckedChanged method of each RadioButton Click event Processing to complete the label switch.

In fact, I had thought about why to use RadioButton instead of ordinary button. Later, by doing their own projects, it was found that using Radiogroup has the following advantages :
1. In addition, it is more convenient to understand the layout, no longer to use LinearLayout and other layout to contain the button.
2. We can easily get the currently selected tags, of course, through the Tabhost Tabhost.getcurrenttabtag () and Getcurrenttab () is also possible.
3. Set up listening is very convenient, just need to set up for radiogroup monitoring on the line, the program's corresponding code is

((Radiogroup) Findviewbyid (r.id.tab_radiogroup))
       . Setoncheckedchangelistener (this);

If we use a button, we need to set up a monitor for all the button, which is relatively troublesome.
4. Perhaps the most important point is because RadioButton itself supports the layout of pictures and text, just specify what pictures and words are, and do not need to implement this layout on our own.

<radiobutton android:id= "@+id/radio_button_more"
   android:layout_margintop= "2.0dip"
android:text= "more"
   android:drawabletop= "@drawable/account05"
   style= "@style/tab_button_bottom"/>

Of course, if RadioButton can't meet our project requirements, like we don't need pictures and don't want text to be displayed on the bottom, but centered, then we can use other components instead of RadioButton. In fact, we can modify or customize, and other ways to achieve a variety of beautiful effects, such as "Renren" mobile phone client's personal home page tab tag can be left and right sliding.

Original Author: men should be like the sea

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.