Use Fragmenttabhost to complete a simple bottom navigation bar

Source: Internet
Author: User

Using Fragmenttabhost+fragment to implement a bottom navigation bar
Main layout:

Place fragment<framelayoutandroid:layout_width="Match_parent"android:layout_height ="Wrap_content"android:layout_weight= "1"android:id="@+id/fragment ">                                    </framelayout>    <android.support.v4.app.FragmentTabHostandroid:id="@+id/tabhost"android: Layout_width="Fill_parent"android:layout_height="Wrap_content">                                <!--<FrameLayout-->            <!--android:id= "@+id/tabcontent" -            <!--android:layout_width= "0DP" --            <!--android:layout_height= "0DP" --            <!--android:layout_weight= "0" -        <!--/>-->    </android.support.v4.app.FragmentTabHost>

Code:

 Public  class fragmenttabhost extends appcompatactivity {    PrivateClass fragmentarray[] = {blankfragment.class, blankfragment1.class, blankfragment2.class};Private intMimageviewarray[] = {R.drawable.tag,r.drawable.thunder,r.drawable.umbrella};PrivateString mtextviewarray[] = {"Home","Message","More"};@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_fragment_tabhost);        Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar);        Setsupportactionbar (toolbar);        Fragmenttabhost fg= (fragmenttabhost) Findviewbyid (r.id.tabhost); Fg.setup ( This, Getsupportfragmentmanager (), r.id.fragment);intCount = Fragmentarray.length;//Loop placement         for(inti =0; I < count;            i++) {Tabhost.tabspec Tabspec = Fg.newtabspec (Mtextviewarray[i]). Setindicator (Gettabitemview (i)); Fg.addtab (Tabspec, Fragmentarray[i],NULL); }//Remove the spacing bar in the bottom navigation barFg.gettabwidget (). Setdividerdrawable (NULL); }//Place the picture and title on the navigation bar    PrivateViewGettabitemview(intIndex) {View view = Layoutinflater.from (Getapplicationcontext ()). Inflate (R.layout.tab_item_xml,NULL);        ImageView ImageView = (ImageView) View.findviewbyid (R.id.imageview);        Imageview.setimageresource (Mimageviewarray[index]);        TextView TextView = (TextView) View.findviewbyid (R.id.textview); Textview.settext (Mtextviewarray[index]);returnView }}

Check the discoloration is not wordy

Use Fragmenttabhost to complete a simple bottom navigation bar

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.