简体   繁体   English

Android 工具栏不显示溢出菜单

[英]Android toolbar not showing overflow menu

I'm new to android apps and I'm hoping someone with keen eyes can spot what I'm doing wrong.我是 android 应用程序的新手,我希望有敏锐眼光的人能发现我做错了什么。 I've tried to follow the steps here , to create a toolbar for my application.我已尝试按照此处的步骤为我的应用程序创建工具栏。 However, it's not showing the three dots overflow menu (which I thought was automatic).但是,它没有显示三点溢出菜单(我认为是自动的)。

Here's what I see:这是我看到的:

在此处输入图像描述

I'm using minSdkVersion 19 .我正在使用minSdkVersion 19

MainAcivitiy.java 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布局\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菜单\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样式.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>

I Guess you missed this我猜你错过了这个

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

if your phone has a physical menu button you can't see three dots如果您的手机有物理菜单按钮,则您看不到三个点

if you want to force to show three dots do as follow:如果你想强制显示三个点,请执行以下操作:

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

Solution: i've tried the following method and i've found the issue.解决方案:我试过下面的方法,我发现了问题。

I think this is refering that theme to Theme.AppCompat.Light that's why this theme has a toolbar.我认为这是将该主题引用到Theme.AppCompat.Light ,这就是该主题具有工具栏的原因。

i just did this and it's working:我刚刚做了这个并且它正在工作:

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

Results:结果:

在此处输入图像描述

it's working.它的工作。 but,但,

With your codes and getSupportActionBar();使用您的代码和getSupportActionBar(); : :

在此处输入图像描述

Note that our Toolbar has a gray color like this:请注意,我们的工具栏具有如下灰色:

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

Ps: Also i want to make sure you'll do that perfectly, as doc said, we have to add this to Manifest : Ps:另外我想确保你能完美地做到这一点,正如医生所说,我们必须将其添加到Manifest

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

and seems like it's not working or we've lost something.BTW, i didn't do that on my manifest and that fixed without above code on manifest.似乎它不起作用或者我们丢失了一些东西。顺便说一句,我没有在我的清单上这样做并且在清单上没有上面的代码的情况下修复了。

Call setHasOptionsMenu(true) in onCreateView .onCreateView中调用setHasOptionsMenu(true)

In case you are inflating your menu from a fragment, eg by overriding onCreateOptionsMenu method.如果您从片段中扩充菜单,例如通过覆盖onCreateOptionsMenu方法。

I had the same problem.我有同样的问题。 I tried everything here and here , and nothing helped.我在这里和这里尝试了一切,但没有任何帮助。 Everything was just like it should be.一切都应该如此。 After whole day of investigating, I found this statement in my onCreate method:经过一整天的调查,我在我的 onCreate 方法中发现了这条语句:

toolbar.setVisibility(View.INVISIBLE);

When I removed it, geuss what happend: 3 dots menu appears now, just like it should.当我删除它时,猜猜发生了什么:现在出现了 3 点菜单,就像它应该的那样。

After following all the answer's suggestions, the overflow button was still not showing.按照所有答案的建议后,溢出按钮仍然没有显示。 This is how I solved it:这就是我解决它的方法:

  • Make sure to override both methods "onPrepareOptionsMenu" and "onCreateOptionsMenu" in your activity确保在您的活动中覆盖“onPrepareOptionsMenu”和“onCreateOptionsMenu”这两种方法
  • Return always true.返回始终为真。

Check return comments for this methods on Activity.java:检查 Activity.java 上此方法的返回注释:

@return You must return true for the menu to be displayed, if you return false it will not be shown. @return 必须返回 true 才能显示菜单,如果返回 false 则不会显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM