【问题标题】:How to show action items at the bottom using Toolbar如何使用工具栏在底部显示操作项
【发布时间】:2015-02-13 10:17:49
【问题描述】:

main.xml

<item
    android:id="@+id/action_back"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:icon="@drawable/ic_action_back"
    android:title="@string/back"/>

<item
    android:id="@+id/action_save"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:icon="@drawable/ic_action_save"
    android:title="@string/save"/>

<item
    android:id="@+id/action_sort"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:icon="@drawable/ic_action_sort_dark"
    android:title="@string/sort"/>

<item
    android:id="@+id/action_new"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:icon="@drawable/ic_new"
    android:title="@string/new_menu"/>

Manifest.xml

<activity
    android:name="com.app.FileFragmentActivity"
    android:uiOptions="splitActionBarWhenNarrow"
    android:label="@string/app_name" >
</activity>

输出:

要求:

我想在底部显示操作项,如上面的两个屏幕截图(标记为红色)。
我正在使用Toolbarusing appcompat-v7 库。

【问题讨论】:

标签: android android-support-library android-appcompat material-design android-toolbar


【解决方案1】:

this post (click) 中所述,android:uiOptions="splitActionBarWhenNarrow" 已在 Lollipop 中删除。虽然这没什么大不了的,因为您可以简单地使用两个Toolbars - 一个在顶部,一个在底部。

下面是一些基本的示例代码:

布局

<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/toolbar_top"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_bottom"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"
        android:layout_alignParentBottom="true"
        android:minHeight="?attr/actionBarSize" />

    <LinearLayout
        android:layout_below="@id/toolbar_top"
        android:layout_above="@id/toolbar_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

代码

private void initToolbars() {
    Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
    setSupportActionBar(toolbarTop);

    Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar_bottom);
    toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch(item.getItemId()){
                case R.id.action_settings:
                    // TODO
                    break;
                // TODO: Other cases
            }
            return true;
        }
    });
    // Inflate a menu to be displayed in the toolbar
    toolbarBottom.inflateMenu(R.menu.menu_main);
}

结果

注意: 您必须手动处理何时显示两个或一个工具栏

【讨论】:

  • 我正在尝试您的代码。但我对 setSupportActionBar(toolbarTop) 有疑问。因为我在片段中使用它。
  • 嗨,FragmentActivity 的一部分。所以你应该在你的活动中调用它。
  • 感谢它完美运行。我还有一个小问题。我们可以从左侧显示底部操作项吗?
  • 无论如何都可以显示所有项目而不会溢出?喜欢他的截图吗?
  • 六个图标,但仍然有很多空白空间...在我的测试设备上几乎 80% 的空白空间和项目仍在溢出菜单中...看这里我已经发布了一个问题@ 987654322@
【解决方案2】:

如果你想做这样的事情(来自docs

或者这个(来自你的问题)

然后使用Bottom Navigation Bar 而不是工具栏。它现在包含在支持库中,并且不难设置。有关详细说明,请参阅此答案:

【讨论】:

  • 这个答案有问题吗?我投了反对票,但没有任何评论可以帮助我解决任何问题。
猜你喜欢
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
相关资源
最近更新 更多