android add two toolbars in the same activity ?

耗尽温柔 提交于 2019-11-30 13:34:07

I had a similar need, the only difference being that I wanted a toolbar on top, and a sort of action bar at the bottom.

The toobar had to be material-compliant, with a title, some buttons, and possibly an action overflow, while the bottom bar had to contain some equally-spaced, always visible buttons, and nothing else.

My implementation follows, tested and working, both on Android 4.x and Android 5.x.

Here's the layout; as you can see, I'm not using Toolbar for the bottom bar, but an ActionMenuView, which is a lower-level widget, that the Toolbar itself uses internally:

<?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.support.v7.widget.Toolbar
        android:id="@+id/page_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"/>

    <android.support.v7.widget.ActionMenuView
        android:id="@+id/bottom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentBottom="true"
        android:background="?attr/colorPrimary"
        android:elevation="4dp" />

    <WebView
        android:id="@+id/page"
        android:layout_below="@id/page_toolbar"
        android:layout_above="@id/bottom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Here's the menu for the bottom bar; nothing different, compared to a regular toolbar menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_list"
        android:icon="@drawable/ic_list"
        android:title="@string/action_list"
        app:showAsAction="always"/>

    <!-- several more items here... -->

</menu>

And here's the code in my activity, that derives from AppCompatActivity; as you can see I exploit the onCreateOptionsMenu callback of the top toolbar to inflate and initialize the bottom bar too. Unfortunately, listeners for bottom bar menu items need to be set individually. But they can share the same onOptionsItemSelected with the top toolbar:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the toolbar menu
    getMenuInflater().inflate(R.menu.web_page_menu, menu);

    // Inflate and initialize the bottom menu
    ActionMenuView bottomBar = (ActionMenuView)findViewById(R.id.bottom_toolbar);
    Menu bottomMenu = bottomBar.getMenu();
    getMenuInflater().inflate(R.menu.obj_menu, bottomMenu);
    for (int i = 0; i < bottomMenu.size(); i++) {
        bottomMenu.getItem(i).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                return onOptionsItemSelected(item);
            }
        });
    }

    return true;
}

A screenshot:

I am not sure this is a totally orthodox implementation, but I hope it can be useful to someone.

Subroto

See the following tutorial Using the Android Toolbar (ActionBar) - Tutorial

Also you can use two toolbars if you want to have two different toolbars, just set layout_gravity of both these toolbars differently, one to have top and other to have bottom as following:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">



    <!-- As the main content view, the view below consumes the entire
        space available using match_parent in both dimensions. -->
    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/circle_blue"/>


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_margin="8dp"
        android:background="@drawable/curved_div_shadow"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways">

    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_margin="8dp"
        android:layout_gravity="bottom"
        android:background="@drawable/curved_div_shadow"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways">

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.CoordinatorLayout>

Also read this Using two toolbars with multiple menus for inflating different menus for both the toolbars.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!