我有类似的需求,唯一的区别是我想要顶部有一个工具栏,底部有一个操作栏。
工具栏必须符合材料要求,有标题、一些按钮,可能还包括一个动作溢出,而底部栏必须包含一些等距、始终可见的按钮,没有别的。
我的实现如下,在 Android 4.x 和 Android 5.x 上经过测试和运行。
这是布局;正如您所看到的,我没有使用 Toolbar 作为底部栏,而是使用 ActionMenuView,它是一个较低级别的小部件,工具栏本身在内部使用:
<?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>
这是底部栏的菜单;与常规工具栏菜单相比,没有什么不同:
<?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>
这是我的活动中的代码,它派生自 AppCompatActivity;如您所见,我利用顶部工具栏的 onCreateOptionsMenu 回调来膨胀并初始化底部栏。不幸的是,底部栏菜单项的侦听器需要单独设置。但它们可以与顶部工具栏共享相同的 onOptionsItemSelected:
@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;
}
截图:
![enter image description here](https://i.stack.imgur.com/HeW5o.png)
我不确定这是否是一个完全正统的实现,但我希望它对某人有用。