Both the bottom and top menus are displayed for android.

Source: Internet
Author: User

Both the top and bottom menus are displayed for android.

  In android development, you need to bring up the top and bottom menus at the same time. Currently, 91 panda.com and QQ reader are available in the listed apps.

Clicking Menu or clicking the screen will bring up the Menu. There are many ways to achieve this. My method is to set the menu layout in RelativaLayout, and then make it

Show/hide. The procedure is as follows:

I. layout. You can make some complex designs as needed. Here we use two buttons btn_top and btn_bottom.

<Button
Android: id = "@ + id/btn_top"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "# AAAAAA"
Android: text = "@ string/top" android: textSize = "30dip"
Android: textColor = "# FF0000"
Android: visibility = "invisible"/> <! -- Hide by default -->
<Button
Android: id = "@ + id/btn_bottom"
Android: layout_height = "wrap_content" android: layout_width = "fill_parent"
Android: background = "# AAAAAA"
Android: text = "@ string/bottom" android: textSize = "30dip"
Android: textColor = "# FF0000"
Android: layout_alignParentBottom = "true"
Android: visibility = "invisible"/> <! -- Hide by default -->

// These two views are placed on the top layer. Set the main layout in the same layout.

2. Set handler in the Code. Because the main thread cannot operate the UI, it can only be implemented through handler.

Private class MainHandler extends Handler {
Static final int MSG_VISIBLE = 1; // display
Static final int MSG_INVISIBLE = 2; // disappears
@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
Animation inAnima = new AlphaAnimation (0.1f, 1.0f); // It indicates the Animation effect when the button is displayed. Can be set as needed
InAnima. setDuration (1000 );
Animation outAnima = new AlphaAnimation (1.0f, 0.1f); // It indicates the Animation effect when the button disappears. Can be set as needed
OutAnima. setDuration (1000 );
Switch (msg. what)
{
Case MSG_VISIBLE:
BtnTop. setAnimation (inAnima); // sets the animation for display.
BtnBottom. setAnimation (inAnima); // sets the animation for display.
BtnTop. setVisibility (View. VISIBLE); // set display
BtnBottom. setVisibility (View. VISIBLE); // set display
Break;
Case MSG_INVISIBLE:
BtnTop. setAnimation (outAnima); // sets the animation when it disappears.
BtnBottom. setAnimation (outAnima); // sets the animation when it disappears
BtnTop. setVisibility (View. INVISIBLE); // The setting disappears.
BtnBottom. setVisibility (View. INVISIBLE); // The setting disappears.
Break;
Default:
Break;
}
}

Public void sendMessage (int nMsg) {// encapsulate the sendMessage function in Handler to improve code simplicity.
Message msg = Message. obtain ();
Msg. what = nMsg;
This. sendMessage (msg );
}
}

3. Set a variable clickCount to indicate whether to display or hide the view.

Private int clickCount = 0; // display an odd number and hide an even number

@ Override
Protected void onCreate (Bundle savedInstanceState) {// It is recommended that the main function be as concise as possible on the left.
Super. onCreate (savedInstanceState );
SetContentView (R. layout. testintentactivity );
BtnTop = (Button) this. findViewById (R. id. btn_top );
BtnBottom = (Button) this. findViewById (R. id. btn_bottom );
Handler = new MainHandler (); // defines handler
SetListener ();
}

Private void setListener (){
BtnTop. setOnClickListener (this );
BtnBottom. setOnClickListener (this );
}

@ Override
Public void onClick (View v ){
ClickCount ++; // The global variable value must be changed.
Switch (v. getId ()){
Case R. id. btn_top:
Toast. makeText (this, getResources (). getString (R. string. top), Toast. LENGTH_SHORT). show (); // pop up Toast to test whether the button gets the focus
Break;
Case R. id. btn_bottom:
Toast. makeText (this, getResources (). getString (R. string. bottom), Toast. LENGTH_SHORT). show (); // pop-up Toast to test whether the button gets the focus
Break;
Default:
Break;
}
Handler. sendMessage (MainHandler. MSG_INVISIBLE); // no matter which button you click. Both buttons must be hidden.
}

4. Listen to the touch screen and use onKeyDown to listen to the Menu key

@ Override
Public boolean onTouchEvent (MotionEvent event ){
If (event. getAction () = MotionEvent. ACTION_UP ){
ClickCount ++;
If (clickCount % 2 = 0) {// hide an even number
Handler. sendMessage (MainHandler. MSG_INVISIBLE );
} Else {// the odd number disappears.
Handler. sendMessage (MainHandler. MSG_VISIBLE );
}
}
Return true;
}

@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_MENU) {// do not use onCreateOptionsMenu to listen to the Menu. It will call some default attributes of the system and will not achieve the desired effect.
ClickCount ++;
If (clickCount % 2 = 0 ){
Handler. sendMessage (MainHandler. MSG_INVISIBLE );
} Else {
Handler. sendMessage (MainHandler. MSG_VISIBLE );
}
}
Return super. onKeyDown (keyCode, event );
}

Both the top and bottom menus are displayed for android.

  In android development, you need to bring up the top and bottom menus at the same time. Currently, 91 panda.com and QQ reader are available in the listed apps.

Clicking Menu or clicking the screen will bring up the Menu. There are many ways to achieve this. My method is to set the menu layout in RelativaLayout, and then make it

Show/hide. The procedure is as follows:

I. layout. You can make some complex designs as needed. Here we use two buttons btn_top and btn_bottom.

<Button
Android: id = "@ + id/btn_top"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "# AAAAAA"
Android: text = "@ string/top" android: textSize = "30dip"
Android: textColor = "# FF0000"
Android: visibility = "invisible"/> <! -- Hide by default -->
<Button
Android: id = "@ + id/btn_bottom"
Android: layout_height = "wrap_content" android: layout_width = "fill_parent"
Android: background = "# AAAAAA"
Android: text = "@ string/bottom" android: textSize = "30dip"
Android: textColor = "# FF0000"
Android: layout_alignParentBottom = "true"
Android: visibility = "invisible"/> <! -- Hide by default -->

// These two views are placed on the top layer. Set the main layout in the same layout.

2. Set handler in the Code. Because the main thread cannot operate the UI, it can only be implemented through handler.

Private class MainHandler extends Handler {
Static final int MSG_VISIBLE = 1; // display
Static final int MSG_INVISIBLE = 2; // disappears
@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
Animation inAnima = new AlphaAnimation (0.1f, 1.0f); // It indicates the Animation effect when the button is displayed. Can be set as needed
InAnima. setDuration (1000 );
Animation outAnima = new AlphaAnimation (1.0f, 0.1f); // It indicates the Animation effect when the button disappears. Can be set as needed
OutAnima. setDuration (1000 );
Switch (msg. what)
{
Case MSG_VISIBLE:
BtnTop. setAnimation (inAnima); // sets the animation for display.
BtnBottom. setAnimation (inAnima); // sets the animation for display.
BtnTop. setVisibility (View. VISIBLE); // set display
BtnBottom. setVisibility (View. VISIBLE); // set display
Break;
Case MSG_INVISIBLE:
BtnTop. setAnimation (outAnima); // sets the animation when it disappears.
BtnBottom. setAnimation (outAnima); // sets the animation when it disappears
BtnTop. setVisibility (View. INVISIBLE); // The setting disappears.
BtnBottom. setVisibility (View. INVISIBLE); // The setting disappears.
Break;
Default:
Break;
}
}

Public void sendMessage (int nMsg) {// encapsulate the sendMessage function in Handler to improve code simplicity.
Message msg = Message. obtain ();
Msg. what = nMsg;
This. sendMessage (msg );
}
}

3. Set a variable clickCount to indicate whether to display or hide the view.

Private int clickCount = 0; // display an odd number and hide an even number

@ Override
Protected void onCreate (Bundle savedInstanceState) {// It is recommended that the main function be as concise as possible on the left.
Super. onCreate (savedInstanceState );
SetContentView (R. layout. testintentactivity );
BtnTop = (Button) this. findViewById (R. id. btn_top );
BtnBottom = (Button) this. findViewById (R. id. btn_bottom );
Handler = new MainHandler (); // defines handler
SetListener ();
}

Private void setListener (){
BtnTop. setOnClickListener (this );
BtnBottom. setOnClickListener (this );
}

@ Override
Public void onClick (View v ){
ClickCount ++; // The global variable value must be changed.
Switch (v. getId ()){
Case R. id. btn_top:
Toast. makeText (this, getResources (). getString (R. string. top), Toast. LENGTH_SHORT). show (); // pop up Toast to test whether the button gets the focus
Break;
Case R. id. btn_bottom:
Toast. makeText (this, getResources (). getString (R. string. bottom), Toast. LENGTH_SHORT). show (); // pop-up Toast to test whether the button gets the focus
Break;
Default:
Break;
}
Handler. sendMessage (MainHandler. MSG_INVISIBLE); // no matter which button you click. Both buttons must be hidden.
}

4. Listen to the touch screen and use onKeyDown to listen to the Menu key

@ Override
Public boolean onTouchEvent (MotionEvent event ){
If (event. getAction () = MotionEvent. ACTION_UP ){
ClickCount ++;
If (clickCount % 2 = 0) {// hide an even number
Handler. sendMessage (MainHandler. MSG_INVISIBLE );
} Else {// the odd number disappears.
Handler. sendMessage (MainHandler. MSG_VISIBLE );
}
}
Return true;
}

@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_MENU) {// do not use onCreateOptionsMenu to listen to the Menu. It will call some default attributes of the system and will not achieve the desired effect.
ClickCount ++;
If (clickCount % 2 = 0 ){
Handler. sendMessage (MainHandler. MSG_INVISIBLE );
} Else {
Handler. sendMessage (MainHandler. MSG_VISIBLE );
}
}
Return super. onKeyDown (keyCode, event );
}

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.