Android工具栏未显示溢出菜单

7

我是一个新手,刚开始接触安卓应用程序,希望有经验的人能够发现我的问题。我尝试按照这里的步骤为我的应用程序创建一个工具栏。然而,它没有显示出三个点的溢出菜单(我以为这是自动的)。

这是我看到的:

enter image description 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>

6个回答

18

我猜你错过了这个

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

9
直到2016年底,培训文档中仍然没有这个重要的注意事项。也许需要更新一下? - Tim Maher-De Troyer
命令行开发者在 import android.view.Menu; 之前插入。 - gotwo

2

如果你的手机有一个物理菜单按钮,你可能看不到三个点

如果你想强制显示三个点,请按照以下步骤操作:

<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>

1
onCreateView中调用setHasOptionsMenu(true)
如果您正在从片段中填充菜单,例如通过覆盖onCreateOptionsMenu方法。

1
解决方案:我尝试了以下方法并找到了问题所在。
我认为这是将主题引用到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>

结果:

结果:

enter image description here

它正在运作。 但是,

使用你的代码和 getSupportActionBar();

enter image description here

请注意,我们的工具栏颜色是灰色的,如下所示:
<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" />

附言:另外,我希望确保您能够完美地完成这项工作,正如文档所说,我们需要将此添加到清单中:

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

似乎它没有起作用或我们失去了一些东西。顺便说一下,我没有在我的清单上这样做,而且没有上面的代码也可以修复。

0

我有同样的问题。我在这里和 这里尝试了所有方法,但是没有帮助。一切都应该是正确的。经过整整一天的调查,我在我的onCreate方法中发现了这个语句:

toolbar.setVisibility(View.INVISIBLE);

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

0

在遵循所有答案建议后,溢出按钮仍未显示。这是我解决的方法:

  • 确保在您的活动中覆盖“onPrepareOptionsMenu”和“onCreateOptionsMenu”两种方法
  • 始终返回true。

检查Activity.java中这些方法的返回注释:

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


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