操作栏未在活动中显示

问题描述 投票:0回答:2

我编写了一个简单的菜单并将其添加到操作栏,但我不明白为什么它们在我的活动中不可见。 我正在使用 java 和 android studio,谢谢。

这是我的代码:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.add,menu);
        return super.onCreateOptionsMenu(menu);
}
    

清单文件是:

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyNote"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

menu.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:enabled="false"
        android:icon="@drawable/baseline_add_"
        android:title="@string/add"
        app:showAsAction="always" />
</menu>

这就是结果:

(https://i.stack.imgur.com/P6OvG.png)

java android menu android-actionbar
2个回答
0
投票

您可以查看themes.xml中的主要样式来检查操作栏是否被关闭。

您的主要样式名称应该类似于 Theme.YourProjectName

themes.xml 应位于您的资源根的值包内

这是不显示操作栏的示例

<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.NoActionBar">

这是为您的应用程序显示操作栏的示例

 <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

0
投票

在 AndroidManifest.xml 文件中,在 Application 标记内替换此行,

 android:theme="@style/Theme.MyNote"

与,

 android:theme="@style/Theme.AppCompat.Light.DarkActionBar"

试试这个,我想它会对你有用,我最近遇到了同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.