显示工具栏菜单按钮溢出 [英] Showing a menu overflow button in a Toolbar
问题描述
我只是瞎搞与Android,并试图通过建立一个虚拟应用来学习的东西。我创建了一个棒棒糖工具栏,但我似乎无法弄清楚如何溢出按钮添加到工具栏。我不打算使用V7应用程序兼容性工具栏,因为我想明确地使用工具栏小工具。我希望它看起来是这样的:
结果
main_activity.xml
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:FAB =http://schemas.android.com/apk/res-auto
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直><工具栏的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =112dp
机器人:海拔=4DP
机器人:layout_alignParentTop =真
安卓了minHeight =机器人:ATTR / actionBarSize
机器人:背景=机器人:ATTR / colorPrimary
机器人:重力=底
机器人:paddingBottom会=16DP/>
< / RelativeLayout的>
主要类
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_quotebook);
。getWindow()addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
。getWindow()setStatusBarColor(getResources()的getColor(R.color.primary_dark));
。getWindow()setNavigationBarColor(getResources()的getColor(R.color.primary_dark)); initToolbar();}私人无效initToolbar(){
mToolbar =(工具栏)findViewById(R.id.toolbar);
mToolbar.setTitleTextColor(Color.WHITE);
mToolbar.setTitle(R.string.app_shortname);
mToolbar.showOverflowMenu();
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.menu_overflow,菜单);
返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId(); // noinspection SimplifiableIfStatement
如果(ID == R.id.action_settings){
返回true;
} 返回super.onOptionsItemSelected(项目);
}
menu.xml文件
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto><项目
机器人:ID =@ + ID / action_settings
机器人:orderInCategory =100
机器人:标题=@字符串/ action_settings
机器人:showAsAction =总是
机器人:可见=真/>&所述; /菜单>
如果你想在你应该设置溢出菜单添加项目从不
为的android:showAsAction
。如果设置总是
该项目将在工具栏上的图标。
修改 menu.xml文件
是这样的:
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto> <项目
机器人:ID =@ + ID / action_settings
机器人:orderInCategory =100
机器人:标题=@字符串/ action_settings
机器人:showAsAction =从不
机器人:可见=真/>&所述; /菜单>
此外,你需要调用 setActionBar()
。编辑 initToolbar()
是这样的:
私人无效initToolbar(){
mToolbar =(工具栏)findViewById(R.id.toolbar);
mToolbar.setTitleTextColor(Color.WHITE);
mToolbar.setTitle(gnappo);
mToolbar.showOverflowMenu();
setActionBar(mToolbar);
}
I'm just messing around with Android and trying to learn stuff by building on a dummy app. I've created a Toolbar for Lollipop, but I can't seem to figure out how to add an overflow button to the toolbar. I'm not going to be using the v7 appcompat toolbar, as I want to explicitly use the Toolbar widget. I want it to look something like this:
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="112dp"
android:elevation="4dp"
android:layout_alignParentTop="true"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:gravity="bottom"
android:paddingBottom="16dp"/>
</RelativeLayout>
Main class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quotebook);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.primary_dark));
getWindow().setNavigationBarColor(getResources().getColor(R.color.primary_dark));
initToolbar();
}
private void initToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitleTextColor(Color.WHITE);
mToolbar.setTitle(R.string.app_shortname);
mToolbar.showOverflowMenu();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_overflow, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:showAsAction="always"
android:visible="true"/>
</menu>
If you want to add the item in the overflow menu you should set never
as android:showAsAction
. If you set always
the item will be an icon in the toolbar.
Edit your menu.xml
like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:showAsAction="never"
android:visible="true" />
</menu>
Also you need to call setActionBar()
. Edit initToolbar()
like this:
private void initToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitleTextColor(Color.WHITE);
mToolbar.setTitle("gnappo");
mToolbar.showOverflowMenu();
setActionBar(mToolbar);
}
这篇关于显示工具栏菜单按钮溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!