Android-based custom menu and navigation bar at the bottom (1)

Source: Internet
Author: User

Because the Android platform is split into different versions, OMS has modified the menu items of Android to adapt to the model as much as possible. We need to define the menu items in the project.



Package COM. unimob; import Java. util. arraylist; import Java. util. hashmap; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. content. dialoginterface. onkeylistener; import android. OS. bundle; import android. view. gravity; import android. view. keyevent; import android. view. menu; import android. view. view; import android. view. window; import android. widge T. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. gridview; import android. widget. listview; import android. widget. simpleadapter; import android. widget. toast; public class mainmenuview extends activity {alertdialog menudialog; // menu dialog listview; gridview menugrid, toolbargrid; view menuview; private Boolean ismore = false; // menu page flip control/* -- menu selection Item subscript -- */private final int item_search = 0; // search private final int item_file_manager = 1; // file management private final int item_down_manager = 2; // download management private final int item_fullscreen = 3; // full screen private final int item_more = 11; // menu/* -- subscript of menu options at the bottom of toolbar -- */private final int toolbar_item_pagehome = 0; // homepage private final int toolbar_item_back = 1; // retreat private final int toolbar_item_forward = 2; // forward PR Ivate final int toolbar_item_new = 3; // create private final int toolbar_item_menu = 4; // menu/** menu image **/INT [] menu_image_array = {R. drawable. menu_search, R. drawable. menu_filemanager, R. drawable. menu_downmanager, R. drawable. menu_fullscreen, R. drawable. menu_inputurl, R. drawable. menu_bookmark, R. drawable. menu_bookmark_sync_import, R. drawable. menu_sharepage, R. drawable. menu_quit, R. drawable. menu_nightmo De, R. drawable. menu_refresh, R. drawable. menu_more};/** menu text **/string [] menu_name_array = {"Search", "File Management", "download management", "full screen", "url ", "bookmarks", "add bookmarks", "share pages", "exit", "Night mode", "refresh", "more "}; /** menu Image 2 **/INT [] menu_image_array2 = {R. drawable. menu_auto_landscape, R. drawable. menu_penselectmodel, R. drawable. menu_page_attr, R. drawable. menu_novel_mode, R. drawable. menu_page_updown, R. drawable. menu_checkupdate, R. dr Awable. menu_checknet, R. drawable. menu_refreshtimer, R. drawable. menu_syssettings, R. drawable. menu_help, R. drawable. menu_about};/** menu text 2 **/string [] menu_name_array2 = {"automatic landscape screen", "pen selection mode", "Read mode", "browsing mode ", "Quick flip", "check for updates", "check for networks", "timed refresh", "Settings", "help", "about "}; /** bottom menu image **/INT [] menu_toolbar_image_array = {R. drawable. controlbar_homepage, R. drawable. controlbar_backward_enable, R. drawable. controlbar_for Ward_enable, R. drawable. controlbar_window, R. drawable. controlbar_showtype_list};/** bottom menu text **/string [] menu_toolbar_name_array = {"Homepage", "back", "forward", "CREATE", "menu "}; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // set the custom menu menuview = view. inflate (this, R. layout. gridview_menu, null); // menuview. set // create alertdialog Menudialog = new alertdialog. builder (this ). create (); window W = menudialog. getwindow (); W. setgravity (gravity. bottom); // windowmanager. layoutparams Lp = W. getattributes (); menudialog. setview (menuview); menudialog. setonkeylistener (New onkeylistener () {public Boolean onkey (dialoginterface dialog, int keycode, keyevent event) {If (keycode = keyevent. keycode_menu) // listens for the dialog button. dismiss (); Return Fal Se ;}}); menugrid = (gridview) menuview. findviewbyid (R. id. gridview); menugrid. setadapter (getmenuadapter (menu_name_array, menu_image_array);/** monitor menu options **/menugrid. setonitemclicklistener (New onitemclicklistener () {public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {Switch (arg2) {Case item_search: // search for break; Case item_file_manager: // file management break; Case item_down_manager: // download and manage break; Case item_fullscreen: // full screen break; Case item_more: // flip the page if (ismore) {menugrid. setadapter (getmenuadapter (menu_name_array2, menu_image_array2); ismore = false;} else {// home page menugrid. setadapter (getmenuadapter (menu_name_array, menu_image_a Rray); ismore = true;} menugrid. invalidate (); // update menu menugrid. setselection (item_more); break ;}}); // create the toolbar toolbargrid = (gridview) findviewbyid (R. id. gridview_toolbar); toolbargrid. setbackgroundresource (R. drawable. channelgallery_bg); // sets the background toolbargrid. setnumcolumns (5); // set the number of columns in each row toolbargrid. setgravity (gravity. center); // position center toolbargrid. setverticalspacing (10); // vertical interval toolbargrid. s Ethorizontalspacing (10); // horizontal interval toolbargrid. setadapter (getmenuadapter (menu_toolbar_name_array, menu_toolbar_image_array); // you can set the menu adapter, ** listener bottom menu options **, and toolbargrid. setonitemclicklistener (New onitemclicklistener () {public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {toast. maketext (mainmenuview. this, menu_toolbar_name_array [arg2], toast. length_short ). show (); Switch (arg2) {Case toolbar_item_pagehome: break; Case toolbar_item_back: break; Case toolbar_item_forward: break; Case toolbar_item_new: break; Case toolbar_item_menu: menudialog. show (); break ;}});/** listview list **/listview = (listview) findviewbyid (R. id. listview_catalog); listview. setadapter (getmenuadapter (menu_name_array2, menu_image_array2);} @ override/*** create menu */Public Boolean oncreateoptionsmenu (menu) {menu. add ("menu"); // You must create a return Super. oncreateoptionsmenu (menu);} @ override/*** intercept menu */Public Boolean onmenuopened (INT featureid, menu) {If (menudialog = NULL) {menudialog = new alertdialog. builder (this ). setview (menuview ). show ();} else {menudialog. show () ;}return false; // if the return value is true, the system menu is displayed.}/*** structure menu adapter ** @ Param menunamearray name * @ Param imageresourcearray image * @ return simpleadapter */private simpleadapter getmenuadapter (string [] menunamearray, int [] imageresourcearray) {arraylist 



<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><GridView         android:id="@+id/gridview"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:numColumns="4"         android:verticalSpacing="10dip"         android:horizontalSpacing="10dip"         android:stretchMode="columnWidth"         android:gravity="bottom"         />  </LinearLayout>



<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "" Android: Id = "@ + ID/relativelayout_item" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: paddingbottom = "5dip"> <imageview Android: layout_height = "wrap_content" Android: Background = "@ drawable/icon" Android: Id = "@ + ID/imageview1" Android: layout_width = "wrap_content" Android: layout_centerhorizontal = "true"> </imageview> <imageview Android: Id = "@ + ID/item_image" Android: layout_centerhorizontal = "true" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/menu_about"> </imageview> <textview Android: layout_below = "@ ID/item_image" Android: Id = "@ + ID/item_text" Android: layout_centerhorizontal = "true" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "option"> </textview> </relativelayout>

Main. xml


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=""android:id="@+id/RelativeLayout_catalog" android:layout_width="fill_parent"android:layout_height="wrap_content"><GridView android:id="@+id/GridView_toolbar"android:layout_height="wrap_content" android:layout_width="fill_parent"android:layout_alignParentBottom="true"></GridView><ListView android:id="@+id/ListView_catalog"android:layout_above="@id/GridView_toolbar" android:layout_width="fill_parent"android:layout_height="wrap_content"></ListView></RelativeLayout>


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: 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.