Android: Improving customized Menu (TabMenu) in 18 articles)

Source: Internet
Author: User
Tags transparent color

The Chinese New Year is approaching. I would like to wish you a happy New Year!
People who have used UCWEB-Android should be impressed with their special menus and make the menus into Tab-menus (paging menus supported ), it can accommodate richer content than the traditional menu of Android (more menu items of Android are scaled down in [more]). For more information, see the example on the Internet (by CoffeeCole, email: to simplify and encapsulate the example so that it can be integrated into your own framework as a composite control.

Let's take a look at the effect of the program running in this article:

TabMenu itself is a PopupWindow, and two GridView are put on the PopupWindow. The first GridView is a page tag, which is located at the top of the PopupWindow, and the second GridView is the menu, which is located at the main body of the PopupWindow. This article uses the following code to implement the pop-up/exit animation of PopupWindow:

Add the anim sub-directory to the res folder of the project, and then create the file popup_enter.xml:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "">
<Translate android: fromYDelta = "100% p" android: toYDelta = "0" android: duration = "1000"/>
<Alpha android: fromAlpha = "0.0" android: toAlpha = "1.0" android: duration = "1000"/>

Create the popup_exit.xml file:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "">
<Translate android: fromYDelta = "0" android: toYDelta = "100% p" android: duration = "1000"/>
<Alpha android: fromAlpha = "1.0" android: toAlpha = "0.0" android: duration = "1000"/>

Create the file popup_animation.xml in the project's values Folder:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Style name = "PopupAnimation" parent = "android: Animation">
<Item name = "android: windowEnterAnimation"> @ anim/popup_enter </item>
<Item name = "android: javaswexitanimation"> @ anim/popup_exit </item>


The source code of main. xml 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 = "">
<TextView android: id = "@ + id/TextView01" android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "extended Menu ---- hellogv"> </TextView>

The source code of TabMenu. java 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); // The TRANSPARENT Color is 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); // The TRANSPARENT Color is 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); // you can specify the background of a TabMenu.
This. setAnimationStyle (aniTabMenu );
This. setFocusable (true); // The menu gets the focus. If the focus is not obtained, the control event in the 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 );
* Custom Adapter, the subject of each page of TabMenu
Static public class MenuBodyAdapter extends BaseAdapter {
Private Context mContext;
Private int fontColor, fontSize;
Private String [] texts;
Private int [] resID;
* Set the pagination subject of TabMenu
* @ Param context the context of the caller
* @ Param texts: String Array of the set of buttons
* @ Param resID: array of ICON resources in the set of buttons
* @ Param fontSize: font size of the button
* @ Param color: font color of the button
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 (pos

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.