Android 工具栏不显示溢出菜单

Android toolbar not showing overflow menu

我是 android 应用程序的新手,我希望有敏锐眼光的人能够发现我做错了什么。我已尝试按照 here 的步骤为我的应用程序创建工具栏。但是,它没有显示三点溢出菜单(我认为是自动的)。

这是我看到的:

我正在使用 minSdkVersion 19.

MainAcivitiy.java

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;


public class MainActivity extends AppCompatActivity {

@SuppressLint("ShowToast")
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);

layout\main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

menu\main.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"
    app:showAsAction="never"
    android:visible="true" />

styles.xml

<resources>
<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

我猜你错过了这个

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

如果您的 phone 有物理菜单按钮,您将看不到三个点

如果要强制显示三个点,请执行以下操作:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/menu"
    app:showAsAction="always"
    android:icon="@drawable/ic_action_overflow"
    android:visible="true" >
 <menu>
    <item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never"
    android:visible="true" />
 </menu>
</item>

解决方法:我试过下面的方法,发现问题所在。

我认为这是指该主题 Theme.AppCompat.Light 这就是该主题具有工具栏的原因。

我刚刚做了这个并且它正在工作:

<resources>
    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

结果:

它正在工作。 但是,

使用您的代码和 getSupportActionBar();:

请注意,我们的工具栏有这样的灰色:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/darker_gray"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

P.s: 另外我想确保你能完美地做到这一点,正如医生所说,我们必须将其添加到 Manifest

<application
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    />

似乎它不起作用或者我们丢失了 something.BTW,我没有在我的清单上这样做,并且在清单上没有上述代码的情况下修复了。

我遇到了同样的问题。我在这里和 here 尝试了所有方法,但没有任何帮助。一切都应该如此。经过一整天的调查,我在我的 onCreate 方法中发现了这条语句:

toolbar.setVisibility(View.INVISIBLE);

当我删除它时,猜猜发生了什么:现在出现了 3 点菜单,就像它应该的那样。

按照所有答案的建议,溢出按钮仍然没有显示。我是这样解决的:

  • 确保在 activity
  • 中重写方法 "onPrepareOptionsMenu" 和 "onCreateOptionsMenu"
  • Return 总是正确的。

在 Activity.java 上检查 return 对此方法的评论:

@return 必须 return true 才能显示菜单,如果 return false 则不会显示。

onCreateView 中调用 setHasOptionsMenu(true)

以防您从片段中扩充菜单,例如通过覆盖 onCreateOptionsMenu 方法。