简体   繁体   中英

Android Constant Menu for app

I've been looking around on this site and on Google and haven't found anything that really gives me a clear answer. So I thought I'd just ask. I'm new to Android so a clear explanation would be best.

The question is simple. I want my app to have a constant menu at the bottom that I can use at any point, along with the activity behind it.

For example, the score mobile app does this and the red arrow points to what I'd want:

捕获

Or Even this works with the bigger, grey menu:

捕获

Please help.

You can achieve this by using Fragments. http://developer.android.com/guide/topics/fundamentals/fragments.html

When your app starts up you load a single Activity called HomeActivity or whatever. This Activity should load a layout looking like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/relativeLayout1"
        android:layout_alignParentTop="true" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >

        <Button
            android:id="@+id/btnFirst"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="@string/btn_previous" />

        <Button
            android:id="@+id/btnSecond"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/btn_pause" />

        <Button
            android:id="@+id/btnThird"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="@string/btn_next" />
    </RelativeLayout>

</RelativeLayout>

As you see, your 3 buttons are ALWAYS on the bottom of the screen. Instead of calling different Activities you replace the Fragment in the RelativeLayout "fragment_container" with the Fragment you want to display. That means that you have to change your Activity-Classes with Fragment-Classes. Fragments and Activities are quiet similar and it shouldnt be very much hard to change your code.

In Android, the preferred method for displaying navigation and action items like that is to use the ActionBar, which has a mode called "split action bar", where it can be split across the top and bottom of the screen (typically it's just along the top). This is designed specifically for situations like the one you're describing. You can read about the design guidelines for the action bar in the Android Design Guide .

To create the split action bar, from the Dev Guide ,

To enable split action bar, simply add uiOptions="splitActionBarWhenNarrow" to your or manifest element.

Keep in mind that according to the design guide, navigation elements should be along the top bar, and actions (play, pause, email, whatever) should be along the bottom bar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM