Menu Bar implementation at the bottom of android

Source: Internet
Author: User

In Android development, menus must be displayed at the bottom of the screen, but Android itself does not provide such controls. Therefore, you need to write your own code to achieve this. Google the webpage on this topic on the Internet, in the end, it is all copied from the "android implementation bottom menu bar" (the source code is here), but if you copy all the code in this article, you will find that you cannot see the bottom menu bar in the simulator. What is the problem? Check the following section in the/res/layout/main. xml file:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "pixel PX">
</LinearLayout>

The problem lies in the red line above, where the height of the content area in the middle of the screen is written to death. If the screen height of your mobile phone is not that high, obviously, the menu bar is squeezed out at the bottom of the screen.

It is actually very easy to solve this problem. First, call the getdefadisplay display () method of WindowManager to obtain the screen height, and then subtract the height of the Activity Titlebar and the height of the menu bar to be displayed, the remaining difference should be the height of the content area. The code example is as follows:

Modify the preceding layout as follows:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "10px"
Android: id = "@ + id/contentBody">
<TextView android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: gravity = "center" android: textSize = "20px"
Android: layout_gravity = "center" android: text = "content area"/>
</LinearLayout>

The layout_height value of LinearLayout can be set to a valid value.

Write the code for setting the LinearLayout height in the onCreate method of the Activity, as shown below:

WindowManager windowManager = getWindowManager ();
Display display = windowManager. getDefaultDisplay ();
Int screenHeight = display. getHeight (); // screen height

Int titlebarHeight; // The height of the Activity title bar

Int menuBarHeight; // The height of the bottom menu bar

Int topContentHeight; // The height of the content area at the top (if there is other content at the top of the contentBody)

LinearLayout bodyLayout = (LinearLayout) findViewById (R. id. contentBody );
LinearLayout. LayoutParams layParams = (android. widget. LinearLayout. LayoutParams) bodyLayout. getLayoutParams ();
LayParams. height = screenHeight-titlebarHeight-menuBarHeight-topContentHeight;
BodyLayout. setLayoutParams (layParams); // you can specify the height of the contentBody.

Shows the final effect:

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.