android - 工具栏不显示溢出菜单

标签 android android-layout android-toolbar

我一直在关注 Android 开发者页面上的“设置应用程序栏”链接 here .问题是我似乎无法正确完成,屏幕显示一些没有溢出菜单的奇怪工具栏。我已经阅读了一些其他答案并澄清,我正在使用 Moto G2 进行测试。它在硬件方面不包含选项按钮。

我的代码如下:1。主 Activity .java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}}
  1. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.toolbardemo.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/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"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

  1. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.toolbardemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Android 开发者页面没有提到任何关于创建菜单 xml 和其他内容的内容,所以我认为至少应该显示基本设置。

这是我看到的图片:No Overflow menu

最佳答案

将您的 RelativeLayout 更改为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.toolbardemo.MainActivity">

并且您需要将此代码添加到您的 Activity 中

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

关于android - 工具栏不显示溢出菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41304503/

相关文章:

android - 在WebViewClient的某个页面隐藏工具栏向上按钮

android - "No label views point to this text field"警告信息的含义

android - 如何在我的应用程序中将我的个人键盘设置为标准输入

java - 当我第一次启动应用程序时,用户名是空的

android - 我怎样才能把元素放在另一个元素旁边?

android - 不使用 Marshmallow 调用 Spinner onItemSelected

android - 谷歌分析上的自定义 Diamention 不起作用

android - 使用底部导航栏的 Intent 时淡入淡出幻灯片动画

android - 反转 LinearLayout 的方向

android - 升级到 AppCompat v21 后工具栏和状态栏上的绘图错误