Android Fragment+fragmenttabhost Component Implementation Common home page (imitation microblogging sina) _android

Source: Internet
Author: User

The approach is to fragment+fragmenttabhost components to achieve this common app home page effect

First give the Main.xml file

  <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Match_parent " > <framelayout android:id= "@+id/realtabcontent" android:layout_width= "Fill_parent" android:layout_height= "0dip" android:layout_weight= "1" android:background= "@color/white"/> <linearlayout android:layout_wi Dth= "Match_parent" android:layout_height= "wrap_content" android:layout_gravity= "bottom" android:orientation= "vert" ical "> <view android:layout_width=" match_parent "android:layout_height=" 1px "android:background=" @c Olor/color_home_tab_line "/> <android.support.v4.app.fragmenttabhost android:id=" @android: Id/tabhost "and Roid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:background= "@color/et_divider_ Disable "> <framelayout andRoid:id= "@android: Id/tabcontent" android:layout_width= "0DP" android:layout_height= "0DP" Android:layout_weigh
 t= "0"/> </android.support.v4.app.FragmentTabHost>  </LinearLayout> </LinearLayout>

main code:

 public class Mainactivity {@ViewInject (Android).
 R.id.tabhost) Private Fragmenttabhost mtabhost;
 
 Private Layoutinflater Layoutinflater; private int mimageviewarray[] = {R.DRAWABLE.HOME_TAB1, r.drawable.home_tab2, R.DRAWABLE.HOME_TAB3, R.drawable.home_
   TAB4};
   Private String mtextviewarray[] = {"Home page", "Circle", "Information", "Personal Center"};
 
 Private Class fragmentarray[] = {fragment1.class, fragment2.class, fragment3.class,fragment4.class};
     protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
     Init ();} @Override protected void init () {//List=new jsonarray ();
     Layoutinflater=layoutinflater.from (this); Inittabhost ()//Initialize Bottom menu}/** * Initialize bottom toolbar * * private void Inittabhost () {mtabhost = (Fragmenttabhost) fi Ndviewbyid (Android.
     R.id.tabhost);
     Mtabhost.setup (this, Getsupportfragmentmanager (), r.id.realtabcontent);
     int count = Fragmentarray.length; for (int i = 0; i < count; i++) {tabhost.Tabspec Tabspec = Mtabhost.newtabspec (mtextviewarray[i)). Setindicator (Gettabitemview (i));
       Mtabhost.addtab (Tabspec, fragmentarray[i], NULL);
     Mtabhost.gettabwidget (). Getchildat (i). Setbackgroundresource (R.color.white);
     } mtabhost.setcurrenttabbytag (Mtextviewarray[0]);
 Mtabhost.gettabwidget (). setdividerdrawable (NULL); /** * Item Style * @param index number * @return each tab style/private View gettabitemview (int index) {Vie
     W view = layoutinflater.inflate (R.layout.tab_home_item, NULL);
     ImageView ImageView = (imageview) View.findviewbyid (R.id.icon);
     Imageview.setimageresource (Mimageviewarray[index]);
     TextView TextView = (TextView) View.findviewbyid (r.id.name);
     Textview.settext (Mtextviewarray[index]);
   return view; }
 
 
 
 
 }

Through the above articles, I hope to help everyone, thank you for your support to this site!

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.