【问题标题】:Android toolbar not showing overflow menuAndroid工具栏不显示溢出菜单
【发布时间】:2016-01-10 08:08:26
【问题描述】:

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

这是我看到的:

我正在使用 minSdkVersion 19

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

【问题讨论】:

    标签: android android-toolbar


    【解决方案1】:

    我猜你错过了这个

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

    【讨论】:

    • 即使到了 2016 年底,培训文档中也没有此重要说明。也许需要更新?
    • 命令行开发者插入之前:import android.view.Menu;
    【解决方案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>
    

    【讨论】:

      【解决方案3】:

      解决方案:我尝试了以下方法,发现问题。

      我认为这是将该主题引用到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"
          />
      

      似乎它不起作用或者我们丢失了一些东西。顺便说一句,我没有在我的清单上这样做,并且在清单上没有上述代码的情况下已修复。

      【讨论】:

        【解决方案4】:

        onCreateView 中致电setHasOptionsMenu(true)

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

        【讨论】:

          【解决方案5】:

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

          toolbar.setVisibility(View.INVISIBLE);
          

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

          【讨论】:

            【解决方案6】:

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

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

            在 Activity.java 上检查此方法的返回 cmets:

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

            【讨论】: