在工具栏中显示菜单溢出按钮

6
我已经为Lollipop创建了一个工具栏Toolbar,但我似乎无法弄清如何将溢出按钮添加到工具栏中。我不打算使用v7 appcompat工具栏,因为我想明确地使用Toolbar小部件。我希望它看起来像这样:

enter image description here

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>

主类

@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>
2个回答

10

如果您希望将项目添加到溢出菜单中,您应将android:showAsAction设置为never。如果您将其设置为always,则该项目将成为工具栏中的图标。

像这样编辑您的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="never"
        android:visible="true" />

</menu>

您还需要调用setActionBar()。将initToolbar()编辑如下:

private void initToolbar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mToolbar.setTitleTextColor(Color.WHITE);
    mToolbar.setTitle("gnappo");
    mToolbar.showOverflowMenu();
    setActionBar(mToolbar);
}

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Nxt3
如果菜单项为空,则无法显示溢出菜单。如果设置 android:show as action="never",则溢出菜单将显示带有“设置”项。 - Mattia Maestrini
这就是我的说法。我正在使用你给我的东西。甚至在我的工具栏中,我都没有出现溢出菜单图标。 - Nxt3
如果我们必须通过支持操作栏来设置所有内容,那么使用工具栏的问题是什么? - blackHawk

3
如果您的工具栏属于包android.support.v7.widget,请使用setSupportActionBar(mToolbar);代替setActionBar(mToolbar);

如果我们必须通过支持操作栏来设置所有内容,那么使用工具栏的问题是什么? - blackHawk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接