如何使用工具栏在底部显示操作项 [英] How to show action items at the bottom using Toolbar
本文介绍了如何使用工具栏在底部显示操作项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
main.xml
清单.xml
android:uiOptions="splitActionBarWhenNarrow"
已在 Lollipop 中删除.虽然这不是什么大问题,因为您可以简单地使用两个 工具栏
- 一个在顶部,一个在底部.遵循一些基本的示例代码:
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v7.widget.Toolbarandroid: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.Toolbarandroid: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"/><线性布局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() {工具栏toolbarTop = (工具栏) findViewById(R.id.toolbar_top);setSupportActionBar(toolbarTop);工具栏toolbarBottom = (工具栏) findViewById(R.id.toolbar_bottom);toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {@覆盖public boolean onMenuItemClick(MenuItem item) {开关(item.getItemId()){案例 R.id.action_settings://去做休息;//TODO: 其他情况}返回真;}});//扩充一个菜单以显示在工具栏中toolbarBottom.inflateMenu(R.menu.menu_main);}
结果
注意: 处理何时显示两个工具栏或只显示一个是您必须手动执行的操作
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>
Output:
Requirement:
I want to show action items at the bottom like in the two screenshots above (marked in red).
I am using Toolbar
using appcompat-v7
library.
解决方案 As stated in this post (click) android:uiOptions="splitActionBarWhenNarrow"
has been removed in Lollipop. Though this is not that big of a deal since you can simply use two Toolbars
- one at the top and one at the bottom.
Following some basic example code:
Layout
<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>
Code
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);
}
Result
Note: Handling when to show two Toolbars or just one is something you have to do manually
这篇关于如何使用工具栏在底部显示操作项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文