Android Improved custom menu (Tabmenu) Implementation _android

Source: Internet
Author: User
Tags transparent color

Generally used ucweb-android version of the people should have a certain impression of their special menu, the menu made Tab-menu (Support paging menu), can accommodate more than the traditional Android menu richer content ( More than 6 of Android's menu is abbreviated to [more], this article simplifies and encapsulates the example on the basis of the example on the web, making it into its own framework as a composite control.

Let's take a look at the effect of this program running as shown in the following illustration:

Tabmenu itself is a popupwindow,popupwindow with two GridView on it, the first GridView is the Paging tab, located at the top of Popupwindow, and the second GridView is the menu, The body that is located in the Popupwindow. In order to achieve the Popupwindow popup/Exit animation effect, this article uses the following code:

Add Anim subdirectories to the Engineering Res folder, and then create a new file Popup_enter.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
 <translate android:fromydelta=" 100%p "android:toydelta=" 0 "android:duration=
 " 1000 "/> <alpha android:fromalpha= "0.0" android:toalpha= "1.0" android:duration= "1000"/>
</set> 

New file Popup_exit.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
 <translate android:fromydelta=" 0 "android:toydelta=" 100%p "
 android:duration=" 1000 "/>" <alpha android:fromalpha= "1.0" android:toalpha= "0.0" android:duration= "1000"/>
</set> 

Create a new file in the Project Values folder Popup_animation.xml:

<?xml version= "1.0" encoding= "Utf-8"?> 
<resources> <style name= "popupanimation" parent= "   
  Android:animation ">
    <item name=" android:windowenteranimation "> @anim/popup_enter</item> 
    <item name= "Android:windowexitanimation" > @anim/popup_exit</item>  
  </style> 
</ Resources> 

Main.xml source code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout android:id=
"@+id/linearlayout01"
 android: Layout_width= "Fill_parent" android:layout_height= "fill_parent"
 xmlns:android= "http://schemas.android.com/" Apk/res/android ">
 <textview android:id=" @+id/textview01 "
 android:layout_height=" Wrap_content " Android:layout_width= "Fill_parent" android:text= "extended menu----HELLOGV" ></TextView>
</linearlayout >

Tabmenu Packaging class Tabmenu.java source code is as follows:

Package com.testtabmenu;
Import Android.content.Context;
Import Android.graphics.Color;
Import android.graphics.drawable.ColorDrawable;
Import android.view.Gravity;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.GridView;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.PopupWindow;
Import Android.widget.TextView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.LinearLayout.LayoutParams;
 public class Tabmenu extends popupwindow{private GridView gvbody, Gvtitle;
 Private LinearLayout mlayout;
 Private Menutitleadapter Titleadapter; Public Tabmenu (Context Context,onitemclicklistener Titleclick,onitemclicklistener Bodyclick, Menutitleadapter
 Titleadapter,int Colorbgtabmenu,int Anitabmenu) {super (context);
 Mlayout = new LinearLayout (context);
 Mlayout.setorientation (linearlayout.vertical);
 Title option Bar Gvtitle = new GridView (context); Gvtitle.setlayOutparams (New Layoutparams (Layoutparams.fill_parent, layoutparams.wrap_content));
 Gvtitle.setnumcolumns (Titleadapter.getcount ());
 Gvtitle.setstretchmode (Gridview.stretch_column_width);
 Gvtitle.setverticalspacing (1);
 Gvtitle.sethorizontalspacing (1);
 Gvtitle.setgravity (Gravity.center);
 Gvtitle.setonitemclicklistener (Titleclick);
 Gvtitle.setadapter (Titleadapter);
 Gvtitle.setselector (New colordrawable (color.transparent));//When selected for transparent color this.titleadapter=titleadapter;
 Sub-option Bar Gvbody = new GridView (context);
 Gvbody.setlayoutparams (New Layoutparams (layoutparams.fill_parent,layoutparams.wrap_content));
 Gvbody.setselector (New colordrawable (color.transparent));//Transparent Color gvbody.setnumcolumns (4) When selected;
 Gvbody.setstretchmode (Gridview.stretch_column_width);
 Gvbody.setverticalspacing (10);
 Gvbody.sethorizontalspacing (10);
 Gvbody.setpadding (10, 10, 10, 10);
 Gvbody.setgravity (Gravity.center);
 Gvbody.setonitemclicklistener (Bodyclick);
 Mlayout.addview (Gvtitle);
 Mlayout.addview (Gvbody); Set upDefault item This.setcontentview (Mlayout);
 This.setwidth (layoutparams.fill_parent);
 This.setheight (layoutparams.wrap_content); This.setbackgrounddrawable (New colordrawable (Colorbgtabmenu))//Set Tabmenu menu background This.setanimationstyle (aniTabMenu)
 ; This.setfocusable (TRUE);//Menu gets focus if the control event in the Focus menu does not get the response} public void Settitleselect (int index) {Gvtitle.setse
 Lection (index);
 This.titleAdapter.SetFocus (index);
 public void Setbodyselect (int index,int colorselbody) {int count=gvbody.getchildcount (); for (int i=0;i<count;i++) {if (I!=index) (linearlayout) Gvbody.getchildat (i). SetBackgroundColor (
 Color.transparent);
 } ((LinearLayout) gvbody.getchildat (Index)). SetBackgroundColor (Colorselbody);
 public void Setbodyadapter (Menubodyadapter bodyadapter) {gvbody.setadapter (bodyadapter); /** * Custom Adapter,tabmenu The principal * */static public class Menubodyadapter extends Baseadapter {private context M
 context;
 private int fontcolor,fontsize;
 Private string[] texts;Private int[] Resid; /** * Set Tabmenu page principal * @param context caller's contextual * @param the string array of the texts button collection * @param an array of icon resources for the Resid button collection * @param font Size button Font Size * @param color button Font colors */public menubodyadapter (context, string[] texts,int[] resid, int fontsize
  , int fontcolor) {this.mcontext = context;
  This.fontcolor = FontColor;
  this.texts = texts;
  This.fontsize=fontsize;
 This.resid=resid;
 public int GetCount () {return texts.length;
 Public Object getitem (int position) {return makemenybody (position);
 public long getitemid (int position) {return position;
  Private linearlayout makemenybody (int position) {LinearLayout result=new linearlayout (this.mcontext);
  Result.setorientation (linearlayout.vertical); Result.setgravity (gravity.center_horizontal| 
  gravity.center_vertical);
  
  Result.setpadding (10, 10, 10, 10);
  TextView Text = new TextView (this.mcontext);
  Text.settext (Texts[position]);
  Text.settextsize (fontsize); Text.settextcoloR (FontColor);
  Text.setgravity (Gravity.center);
  Text.setpadding (5, 5, 5, 5);
  ImageView img=new ImageView (this.mcontext);
  Img.setbackgroundresource (Resid[position]); Result.addview (Img,new linearlayout.layoutparams, new Layoutparams (LAYOUTPARAMS.WRAP_CONTENT,LAYOUTPARAMS.WRAP_
  CONTENT));
  Result.addview (text);
 return result;
 Public View GetView (int position, View Convertview, ViewGroup parent) {return makemenybody (position); }/** * Custom Adapter,tabmenu tab Part */static public class Menutitleadapter extends Baseadapter {private Contex
    T Mcontext;
    private int fontcolor,unselcolor,selcolor;
    Private textview[] title;
     /** * Set tabmenu title * @param context caller @param string array * @param fontsize font size for the Titles page label * @param fontcolor Font color * @param unselcolor background color of unselected items * @param selcolor background color of selected Items * * Public menutitleadapt ER (context context, string[] titles, int fontsize, int fontcolor,int unselcolor,int SELcolor) {This.mcontext = context;
      This.fontcolor = FontColor;
      This.unselcolor = Unselcolor;
      This.selcolor=selcolor;
      This.title = new Textview[titles.length];
        for (int i = 0; i < titles.length i++) {Title[i] = new TextView (mcontext);
        Title[i].settext (Titles[i]);
        Title[i].settextsize (fontsize);
        Title[i].settextcolor (FontColor);
        Title[i].setgravity (Gravity.center);
      Title[i].setpadding (10, 10, 10, 10);
    } public int GetCount () {return title.length;
    Public Object getitem (int position) {return title[position];
    public long getitemid (int position) {return Title[position].getid (); 
     /** * Set Selected effect */private void SetFocus (int index) {for (int i=0;i<title.length;i++) { if (i!=index) {title[i].setbackgrounddrawable (new colordrawable (Unselcolor));//Set unselected color title[i ].settextcolor (FontColor);/Set font color for unselected items} title[index].setbackgroundcolor (0x00);//Set color Title[index].settextcolor for selected items (selcolor)
      //Set Font color for selected items} public view GetView (int position, view Convertview, ViewGroup parent) {View V;
      if (Convertview = = null) {v = title[position];
      else {v = convertview;
    } return v;

 }
 }
}

The

Testtabmenu describes the definition of the data and the use of Tabmenu, the source code is as follows:

Package com.testtabmenu;
Import android.app.Activity;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import android.view.Gravity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.Toast;
 public class Testtabmenu extends activity {tabmenu.menubodyadapter []bodyadapter=new tabmenu.menubodyadapter[3];
 Tabmenu.menutitleadapter Titleadapter;
 Tabmenu Tabmenu;
 int seltitle=0;
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.main); Set the title of the page bar Titleadapter = new Tabmenu.menutitleadapter (this, new string[] {Common, settings, tools}, 0xff222222,color.lt
 Gray,color.white); Define the contents of each page bar bodyadapter[0]=new tabmenu.menubodyadapter (this,new string[] {"Common 1", "Common 2",}, new int[] {R.DRAWABLE.M
 Enu_test, r.drawable.menu_bookmark},13, 0xFFFFFFFF); Bodyadapter[1]=new Tabmenu.menubodyadapter (ThiS,new string[] {"Setting 1", "Setting 2", "Setting 3"}, new int[] {r.drawable.menu_edit, r.drawable.menu_delete, r.drawable.menu_ful
 lscreen},13, 0xFFFFFFFF); Bodyadapter[2]=new Tabmenu.menubodyadapter (this,new string[] {Tools 1, tools 2, Tools 3, Tools 4}, new int[] {r.drawable.men
 U_copy, R.drawable.menu_cut, R.drawable.menu_normalmode, R.drawable.menu_quit},13, 0xFFFFFFFF); 
   Tabmenu=new Tabmenu (This, new Titleclickevent (), New Bodyclickevent (), titleadapter, 0x55123456,//tabmenu background color
  r.style.popupanimation);//appears with vanishing animation tabmenu.update ();
  Tabmenu.settitleselect (0);
 Tabmenu.setbodyadapter (Bodyadapter[0]); Class Titleclickevent implements onitemclicklistener{@Override public void Onitemclick (adapterview<?> arg0, Vi
  EW arg1, int arg2, long arg3) {seltitle=arg2;
  Tabmenu.settitleselect (ARG2);
 Tabmenu.setbodyadapter (Bodyadapter[arg2]); The class Bodyclickevent implements onitemclicklistener{@Override public void Onitemclick (Adapterview<?> arg0, View arg1, int arg2, long arg3) {tabmenu.setbodyselect (Arg2,color.gray);
  String str= "+string.valueof (seltitle) +" column/n/r "+" +string.valueof (arg2) + "item";
 Toast.maketext (Testtabmenu.this, str, M). Show (); }} @Override/** * Create Menu */public boolean Oncreateoptionsmenu (Menu menu) {menu.add ("menu");//must create a return su
 Per.oncreateoptionsmenu (menu); @Override/** * Intercept Menu */public boolean onmenuopened (int featureid, menu menu) {if (tabmenu!= null) {if (tab
  Menu.isshowing ()) Tabmenu.dismiss ();
  else {tabmenu.showatlocation (Findviewbyid (r.id.linearlayout01), Gravity.bottom, 0, 0); Return false;//to True to display system menu}}

Interested readers can test the example in this article by themselves, and I believe it will help you with your Android program development.

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.