Android Development Tips My menu I'm the Boss (custom menu) _android

Source: Internet
Author: User

The Android SDK itself provides a mechanism for creating menus by default. However, the menu created through this mechanism is very functional, but it is a bit "dirt" in the interface effect. For a program with a gorgeous interface with a bit of "dirt" menu, will make the user feel very strange, and even make the gorgeous interface greatly compromised. In fact, modifying the menu style is a piece of cake for such a flexible and powerful Android system. There are many ways to add a nice menu to a program. In this section, we first introduce a more common approach, which is to implement a custom menu by onkeydown event methods and Popupwindow. As for the use of this technology can design a gorgeous menu effect, it depends on our design, aesthetics, psychological skills.

You can know by using the Options menu described in the 6.1.1 section. By pressing the phone's "menu" key (is the hard button on the phone, different mobile phone "menu" key location will be different), you can pop-up options menu, and then press the "Back" button, the option menu will be closed. To simulate the pop-up and shutdown effects of the Options menu, you only need to listen for the press events of the two keys. And when the "menu" key is pressed, use Popupwindow to pop a window as the simulated Options menu. Let's take a look at the effect of the simulation options menu shown in Figure 6.9.



As you can see from Figure 6.9, there are 3 menu items displayed below the interface: "Home", "my", and "more." The text and images of the My menu item are arranged horizontally, and the text and images on the other two menu items are arranged vertically up and down. In fact, this effect is done by a normal layout file (menu_layout.xml), the code is as follows:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Horizontal" android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:gravity= "Bottom" >
<!--first menu item: "Home"-->
<linearlayout android:id= "@+id/home" android:orientation= "vertical"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
android:background= "@drawable/button_normal_translucent"
android:layout_weight= "1" >
<imageview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:src= "@drawable/home"
android:paddingtop= "5DP"/>
<textview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:text= "Home"
android:gravity= "Center"/>
</LinearLayout>
<!--second menu item: "My"-->
<linearlayout android:orientation= "Horizontal"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
android:background= "@drawable/button_normal" android:layout_weight= "1"
android:gravity= "Center" >
<imageview android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" android:src= "@drawable/mine"/>
<textview android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" android:text= "my"/>
</LinearLayout>
<!--Third menu item
<linearlayout android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:background= "@drawable/button_normal"
android:layout_weight= "1" >
<imageview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:src= "@drawable/more"
android:paddingtop= "18DP"/>
<textview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:text= "more"
android:gravity= "center" android:paddingtop= "5DP"/>
</LinearLayout>
</LinearLayout>


Before writing the above code, don't forget to prepare several related images, for example, this example uses 5 images. Where Button_normal_translucent.png is used for the background (semitransparent effect) of the "Home" menu item, button_normal.png the background for the "my" and "more" menu items. Home.png, Mine.png, and more.png are used for images of these three menu items respectively.

Here's how to write code that listens to the "menu" and "Back" key press actions. Press the back key to work with the following two tasks.

If the Options menu has popped up, close the Options menu. If the Options menu does not pop up or has been turned off, close the current activity directly, that is, call the Finish method.

In order to distinguish between the above two tasks, an int type state variable is set in the program, and the option menu pops up when the 1 is the one, and the 2 indicates that the option menu does not pop. Let's look at the complete implementation code below.

Copy Code code as follows:

Package mobile.android.ch06.custom.menu;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.Gravity;
Import android.view.KeyEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.PopupWindow;
Import Android.widget.Toast;
public class Main extends activity
{
Privatepopupwindow Pop;
Privateview layout;
private int state = 2; State variable, 1: Option menu has popped up, 2: Option Menu not popped
@Override
Publicvoid onCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
@Override
Publicboolean onKeyDown (int keycode, keyevent event)
{
Switch (keycode)
{
Case Keyevent.keycode_menu://Press "menu" key action
The Options menu has popped up and no longer pops up the new window
if (state = = 1)
return false;
Mount Options Menu Layout file
Layout =getlayoutinflater (). Inflate (r.layout.menu_layout, NULL);
Creates a Popupwindow object and pops the window used to display the menu at the specified location
Pop = new Popupwindow (Layout,getwindowmanager ()
. Getdefaultdisplay (). GetWidth (), Getwindowmanager ()
. Getdefaultdisplay (). GetHeight ());
Set the position of the pop-up window
Pop.showatlocation (layout,gravity.bottom, 0, 0);
View home = Layout.findviewbyid (r.id.home);
Add a click event for the first page menu item
Home.setonclicklistener (New Onclicklistener ()
{
@Override
public void OnClick (view view)
{
Toast.maketext (Main.this, "Click Custom menu.", Toast.length_long). Show ();
When you click the Home menu item, close the Options menu
Pop.dismiss ();
Reset state variables
state = 2;
}
});
When the menu pops up, set the state variable to 1 to indicate that the option menu has popped up
state = 1;
return false;
Case Keyevent.keycode_back://Press "Back" button action
if (state = = 1)
{
If the Options menu has popped up, close it
Pop.dismiss ();
Set the state variable to the option menu is off
state = 2;
}
else if (state = 2)
{
If the option menu is not already displayed or is closed, close the current activity directly
Finish ();
}
return false;
}
In addition to the "menu" and "Back" press events, you still need to invoke the OnKeyDown method of the activity class to respond to the pressed events of other keys
Return Super.onkeydown (KeyCode, event);
}
}

The following points should be noted when writing the above code.

For the Option menu, when you click a menu item, you perform some action, and the Options menu closes automatically. To simulate this process. A click event was added for the Home menu item. When you click the Home menu item, a toast message pops up, and the Options menu closes.   The OnKeyDown method should return a constant (either false or True) after the action of pressing the "menu" or "Back" key is done. The Super.onkeydown method cannot be called again, or the default action of the system is performed after the custom menu item action is executed. For example, when the "back" key is pressed, the pop-up menu is closed, and the current activity is also closed. Of course, if you need to invoke the OnKeyDown method of the activity class (i.e. the Super.onkeydown method) in addition to the "menu" and "Back" key presses, you can also respond to other key events in the program, otherwise the program except "menu" And the "Back" key, the other keys are almost not good to make. The Showatlocation method is used for the position of the Control pop-up window. The 1th parameter of the method is a View object. In fact, the Showatlocation method only needs to call the View.getwindowtoken method to get a IBinder object. The 2nd parameter of the Showatlocation method represents the position of the pop-up window. This example sets a pop-up window to appear at the bottom of the screen. The last two parameters represent the horizontal and vertical offsets, respectively. This example is set to 0 to indicate that no offsets occur. As a result, the pop-up window appears at the bottom of the screen, which shows the location of the Options menu.
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.