为什么 Android 中的菜单没有添加到工具栏?

Why is the menu not being added to the Toolbar in Android?

我正在尝试向我的工具栏添加一个简单的菜单,但它没有显示。

这是我的 Activity:

package com.github.crmepham.bluetoothmessenger.activities

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import android.widget.Toolbar
import androidx.appcompat.app.AppCompatActivity


class ConversationsActivity : AppCompatActivity() {

    private var mTopToolbar: Toolbar? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(com.github.crmepham.bluetoothmessenger.R.layout.conversation_list)
        mTopToolbar = findViewById(com.github.crmepham.bluetoothmessenger.R.id.my_toolbar)
        setActionBar(mTopToolbar)
    }

    override fun onPrepareOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(com.github.crmepham.bluetoothmessenger.R.menu.main_menu, menu)

        return super.onCreateOptionsMenu(menu)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(com.github.crmepham.bluetoothmessenger.R.menu.main_menu, menu)
        super.onCreateOptionsMenu(menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.getItemId()

        if (id == com.github.crmepham.bluetoothmessenger.R.id.action_favorite) {
            Toast.makeText(this@ConversationsActivity, "Action clicked", Toast.LENGTH_LONG).show()
            return true
        }

        return super.onOptionsItemSelected(item)
    }
}

conversation_list.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    style="@style/HeaderBar"
    android:elevation="4dp" />

</RelativeLayout>

main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.github.crmepham.bluetoothmessenger.activities.ConversationsActivity">

    <item
        android:id="@+id/action_favorite"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="300"
        android:title="@string/app_name"
        android:visible="true"
        app:showAsAction="always"></item>

</menu>

build.gradle 文件:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.github.crmepham.bluetoothmessenger"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.0.0'
}

谁能帮我理解为什么菜单没有添加到 Toolbar

问题出在您的 "onPrepareOptionsMenu(menu)" 方法中:

    override fun onPrepareOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(com.github.crmepham.bluetoothmessenger.R.menu.main_menu, menu)

    return super.onCreateOptionsMenu(menu)
}

你 return "super.onCreateOptionsMenu(menu)" 而不是 "super.onPrepareOptionsMenu(menu)".

我想如果你改变它,它就会起作用

android.widget.Toolbarclass和setActionBar()方法来自原生框架。 androidxAppCompatActivity及其options菜单方法,只会处理androidx.appcompat.widget.Toolbarclass,需要设置为支持ActionBar,对应setSupportActionBar()方法。

您还需要在 Toolbar 的布局元素中使用完全限定的 class 名称。也就是改成<androidx.appcompat.widget.Toolbar>.