Are you hungry? Wait for app bottom menu (fragment implementation)

Source: Internet
Author: User
Tags getcolor

Fragment implementation of the bottom menu navigation, is now a lot of apps have a function, the effect is to click the menu, the menu to implement the icon and color transformation

There are two main ways to achieve this, one is tabhost and the other is the fragment after Android3.0

Fragment, need to add V4 price pack

Then icon icons then, need to do it yourself, or go to http://www.iconfont.cn/, Ali's Gallery download



There are two icons for the bottom menu, one is the default icon, and one is the icon after the menu is clicked.


Ui:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:custom= "http// Schemas.android.com/apk/res-auto "android:layout_width=" match_parent "android:layout_height=" Match_parent "Androi D:background= "#FFFFFF" android:orientation= "vertical" > <framelayout android:layout_width= "Match_par         Ent "android:layout_height=" 0DP "android:layout_weight=" 3 "android:id=" @+id/fg_content "/> <view android:layout_width= "match_parent" android:layout_height= "0.2DP" android:background= "@co Lor/bg_gray "/> <radiogroup android:layout_width=" match_parent "android:layout_height=" wrap_content "Android:background=" @color/white "android:gravity=" center_vertical "android:orientation=" Horizontal            "> <radiobutton android:id=" @+id/rb_homepage "android:layout_width=" Wrap_content "   android:layout_height= "Wrap_content"         android:button= "@null" android:drawabletop= "@drawable/guide_homepage" android:gravity= "cent            Er_horizontal "android:onclick=" onhomepageclicked "android:text=" @string/fragment_menu_homepage "            Android:textcolor= "@color/tab_text_bg"/> <radiobutton android:id= "@+id/rb_order" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:button= " @null "android:drawabletop=" @drawable/guide_order "android:gravity=" Center_horizontal "an droid:onclick= "onorderclicked" android:text= "@string/fragment_menu_order" android:textcolor= "@color/ Tab_text_bg "/> <radiobutton android:id=" @+id/rb_found "android:layout_width=" Wrap_cont Ent "android:layout_height=" wrap_content "android:button=" @null "android:drawabletop=" @dr     Awable/guide_found "       Android:gravity= "Center_horizontal" android:onclick= "onfoundclicked" android:text= "@string/fr Agment_menu_found "android:textcolor=" @color/tab_text_bg "/> <radiobutton Andro            Id:id= "@+id/rb_my" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:button= "@null" android:drawabletop= "@drawable/guide_my" android:gravity= "Center_horizonta L "android:onclick=" onmyclicked "android:text=" @string/fragment_menu_my "Android:textcolo R= "@color/tab_text_bg"/> </RadioGroup> </LinearLayout>





Business implementation:

Package Com.tsc.activities;import Android.content.intent;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmenttransaction;import Android.view.keyevent;import Android.view.View;import Android.view.window;import Android.widget.radiobutton;import Android.widget.toast;import Com.tsc.fragment.alllinefragment;import Com.tsc.fragment.homepagefragment;import com.tsc.fragment.MyFragment; Import Com.tsc.fragment.orderfragment;import com.tsc.listener.ibtncalllistener;/** * * Main interface class * Fragment is a thing after 3.0, in order to use fragment in the lower version of the Android-support-v4.jar compatibility package, * and fragmentactivity is in this compatibility package, It provides some ways to manipulate fragment, which functions the same as 3.0 and later versions of activity. */public class Mainactivity extends fragmentactivity implements Ibtncalllistener{//radiobutton private RadioButton Mhom    Epage;    Private RadioButton Morder;    Private RadioButton Mfound;    Private RadioButton mMy;    Fragment interface class private Homepagefragment mhomepagefragment;Private Orderfragment morderfragment;    Private Alllinefragment linefragment;        Private Myfragment mmyfragment;    Private Intent Intent;        Private bundle bundle;          Private Ibtncalllistener Mbtncalllistener;        Private Bundle Sendbundle;        Private String account;        Fragmenttransaction ft;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Set Untitled requestwindowfeature (Window.feature_no_title);               Setcontentview (R.layout.activity_main);        Initview ();            Initializes the selected fragment SetSelection (3);         }/** * Initialize components */private void Initview () {mhomepage = (RadioButton) Findviewbyid (r.id.rb_homepage);        Morder = (RadioButton) Findviewbyid (R.id.rb_order);        Mfound = (RadioButton) Findviewbyid (R.id.rb_found);    MMy = (RadioButton) Findviewbyid (r.id.rb_my); */** * Click on the bottom menu bar, icon and text color implementation * @param index */Private void setselection (int index) {resetimg ();        Create fragmenttransaction ft = Getsupportfragmentmanager (). BeginTransaction ();        Hidefragments (FT); Switch (index) {case 0://Transform icon mhomepage.setcompounddrawableswithintrinsicbounds (0, R.dra            wable.guide_homepage_on, 0, 0);                            Transform text color Mhomepage.settextcolor (mhomepage.getresources (). GetColor (R.color.lightblue));                                if (mhomepagefragment== null) {mhomepagefragment = new homepagefragment ();                                   Re-added to Fragmenttransaction ft.add (r.id.fg_content,mhomepagefragment);                }else{ft.show (mhomepagefragment);            } break;            Case 1:orderfragmenttransfermsg ();            Break Case 2:mfound.setcompounddrawableswithintrinsicbounds (0, r.drawable.guide_found_on, 0, 0);             Mfound.settextcolor (Mfound.getresources (). GetColor (R.color.lightblue));                     if (linefragment== null) {linefragment = new alllinefragment ();                 Ft.add (r.id.fg_content,linefragment);                 }else{ft.show (linefragment);            } break;                Case 3:myfragmenttransfermsg ();        Break    } ft.commit (); }/** * Restore default picture */private void resetimg () {mhomepage.setcompounddrawableswithintrinsicbounds (0,r.dr    Awable.guide_homepage, 0, 0);            Mhomepage.settextcolor (Mhomepage.getresources (). GetColor (R.COLOR.TAB_TEXT_BG));        Morder.setcompounddrawableswithintrinsicbounds (0,r.drawable.guide_order, 0, 0);                Morder.settextcolor (Morder.getresources (). GetColor (R.COLOR.TAB_TEXT_BG));        Mfound.setcompounddrawableswithintrinsicbounds (0,r.drawable.guide_found, 0, 0); Mfound.settextcolor (MFOUND.GETREsources (). GetColor (R.COLOR.TAB_TEXT_BG));        Mmy.setcompounddrawableswithintrinsicbounds (0,r.drawable.guide_my, 0, 0);    Mmy.settextcolor (Mmy.getresources (). GetColor (R.COLOR.TAB_TEXT_BG)); }/** * Hide fragment * @param ft * * */private void hidefragments (fragmenttransaction ft) {if (M        Homepagefragment = null) {ft.hide (mhomepagefragment);        } if (morderfragment! = null) {ft.hide (morderfragment);        } if (linefragment! = null) {ft.hide (linefragment);        } if (mmyfragment! = null) {ft.hide (mmyfragment);    }}//select home fragment public void onhomepageclicked (view view) {setselection (0);    }//Select message fragment public void onorderclicked (view view) {setselection (1);    }//Select Address Book Fragment public void onfoundclicked (view view) {setselection (2);    }//Select my fragment public void onmyclicked (view view) {SetSelection (3);}/** * Press ENTER */private long exittime = 0;                @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back && event.getaction () = = Keyevent.action_down) {if (System.currenttimemillis ()-Exittime) > () {Toast.maketext (Getapplicationcontext (), "Press again to exit the program", Toast.length_short). Show                ();            Exittime = System.currenttimemillis ();                } else {Movetasktoback (false);            Finish ();        } return true;    } return Super.onkeydown (KeyCode, event);            }/** * Overloaded Onattachfragment method */@Override public void Onattachfragment (Fragment Fragment) {try {         Mbtncalllistener= (Ibtncalllistener) fragment;    } catch (Exception e) {} super.onattachfragment (fragment); }/** * Abstract method for implementing the Ibtncalllistener interface */@Overridepublic void Myfragmenttransfermsg () {mmy.setcompounddrawableswithintrinsicbounds (0, r.drawable.guide_my_on, 0, 0);            Mmy.settextcolor (Mmy.getresources (). GetColor (R.color.lightblue));                if (mmyfragment = = null) {mmyfragment = new myfragment ();                Transfer the parameter account = Getintent () to the fragment class through the bundle. Getstringextra ("account");    Sendbundle = new Bundle ();    Sendbundle.putstring ("account", account);                                Set arguments mmyfragment.setarguments (Sendbundle);            Ft.add (r.id.fg_content,mmyfragment); } ft.show (mmyfragment);} @Overridepublic void Homepagefragmenttransfermsg () {//TODO auto-generated method stub} @Overridepublic void         Orderfragmenttransfermsg () {morder.setcompounddrawableswithintrinsicbounds (0, r.drawable.guide_order_on, 0, 0);         Morder.settextcolor (Morder.getresources (). GetColor (R.color.lightblue));            if (morderfragment== null) {     Morderfragment = new Orderfragment ();                 Transfer the parameter account = Getintent () to the fragment class through the bundle. Getstringextra ("account");     Sendbundle = new Bundle ();     Sendbundle.putstring ("account", account);                 Set arguments morderfragment.setarguments (Sendbundle);             Ft.add (r.id.fg_content,morderfragment);             }else{ft.show (morderfragment); }} @Overridepublic void Foundfragmenttransfermsg () {//TODO auto-generated method stub}}







Are you hungry? Wait for app bottom menu (fragment implementation)

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.