Home>

1. java code:

package com.tabmenu;
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;
/** Scalable menu
 *
 * @author antking
 ** /
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));//Transparent color when selected
  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 when selected
  gvbody.setnumcolumns (4);
  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 the default item
  this.setcontentview (mlayout);
  this.setwidth (layoutparams.fill_parent);
  this.setheight (layoutparams.wrap_content);
  this.setbackgrounddrawable (new colordrawable (colorbgtabmenu));//Set the tabmenu menu background
  this.setanimationstyle (anitabmenu);
  this.setfocusable (true);//The menu menu gets focus. If it doesn't get focus, the control event in the menu menu cannot respond.
 }
 public void settitleselect (int index)
 {
  gvtitle.setselection (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);
 }
 /**
  * Customize the body of each tab, tabmenu
  *
  * /
 static public class menubodyadapter extends baseadapter {
  private context mcontext;
  private int fontcolor, fontsize;
  private string [] texts;
  private int [] resid;
  /**
   * Set the tab body of the tabmenu
   * @param context caller's context
   * @param texts String array of button collection
   * @param resid Icon resource array for button collection
   * @param fontsize button font size
   * @param color button font color
   * /
  public menubodyadapter (context 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);
  }
 }
 /**
  * Customize the tab and tab part of the adapter, tabmenu
  *
  * /
 static public class menutitleadapter extends baseadapter {
  private context mcontext;
  private int fontcolor, unselcolor, selcolor;
  private textview [] title;
  /**
   * Set the title of the tabmenu
   * @param context caller's context
   * @param titles String array of pagination tags
   * @param fontsize font size
   * @param fontcolor font color
   * @param unselcolor background color of unselected items
   * @param selcolor background color of selected item
   * /
  public menutitleadapter (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 the unselected color
     title [i] .settextcolor (fontcolor);//Set the font color of unselected items
    }
   }
   title [index] .setbackgroundcolor (0x00);//Set the color of the selected item
   title [index] .settextcolor (selcolor);//Set the font color of the selected item
  }
  public view getview (int position, view convertview, viewgroup parent) {
   view v;
   if (convertview == null) {
    v=title [position];
   } else {
    v=convertview;
   }
   return v;
  }
 }
}

2. Java code:

package com.tabmenu;
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;
/**
 * Event interface
 * @author wei
 *
 * /
public class activity01 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 pagination bar
  titleadapter=new tabmenu.menutitleadapter (this, new string [] {"commonly used",    "Settings", "tools"}, 16, 0xff222222, color.ltgray, color.white);
  //Define the content of each page bar
  bodyadapter [0]=new tabmenu.menubodyadapter (this, new string [] {"Common 1", "Common 2",},     new int [] {android.r.drawable.alert_dark_frame,    android.r.drawable.ic_delete}, 13, 0xffffffff);
  bodyadapter [1]=new tabmenu.menubodyadapter (this, new string [] {"Set 1", "Set 2",     "Setting 3"}, new int [] {android.r.drawable.ic_menu_edit,     android.r.drawable.btn_default, android.r.drawable.btn_dropdown}, 13, 0xffffffff);
  bodyadapter [2]=new tabmenu.menubodyadapter (this, new string [] {"tool1", "tool2",     "Tool3", "tool4"}, new int [] {android.r.drawable.ic_media_ff,    android.r.drawable.ic_menu_delete, android.r.drawable.ic_btn_speak_now,     android.r.drawable.edit_text}, 13, 0xffffffff);
  tabmenu=new tabmenu (this,     new titleclickevent (),     new bodyclickevent (),     titleadapter,     0x55123456, //background color of tabmenu
     r.style.popupanimation);//Appears and disappears animation
   tabmenu.update ();
   tabmenu.settitleselect (0);
   tabmenu.setbodyadapter (bodyadapter [0]);
 }
 class titleclickevent implements onitemclicklistener {
  @override
  public void onitemclick (adapterview<?>arg0, view arg1, int arg2,    long arg3) {
   seltitle=arg2;
   tabmenu.settitleselect (arg2);
   tabmenu.setbodyadapter (bodyadapter [arg2]);
  }
 }
 class bodyclickevent implements onitemclicklistener {
  @override
  public void onitemclick (adapterview<?>arg0, view arg1, int arg2,    long arg3) {
   tabmenu.setbodyselect (arg2, color.gray);
   string str="No." + string.valueof (seltitle) + "column/n/r"
   + "First" + string.valueof (arg2) + "term";
   toast.maketext (activity01.this, str, 500) .show ();
  }
 }
 @override
 /**
  * Create menu
  * /
 public boolean oncreateoptionsmenu (menu menu) {
  menu.add ("menu");//must create an item
  return super.oncreateoptionsmenu (menu);
 }
 @override
 /**
  * Intercept menu
  * /
 public boolean onmenuopened (int featureid, menu menu) {
  if (tabmenu!=null) {
   if (tabmenu.isshowing ())
    tabmenu.dismiss ();
   else {
    tabmenu.showatlocation (findviewbyid (r.id.linearlayout01),      gravity.bottom, 0, 0);
   }
  }
  return false;//returns true to display the system menu
 }
}

3. Operation results:

  • Previous Share five useful jquery tips
  • Next Getting started with python3 crawlers and regular expressions