操作栏未显示在“活动”中

action bar not showing in activity

提问人:Heda Lexa 提问时间:10/4/2023 更新时间:10/4/2023 访问量:48

问:

我写了一个简单的菜单并将其添加到操作栏中,但我不明白为什么它们在我的活动中不可见。 我正在使用 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>

菜单 .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 菜单 android-actionbar

评论


答:

0赞 Gordon leung 10/4/2023 #1

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

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

主题 .xml 应该位于资源根的值包中

下面是不显示操作栏的示例

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

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

 <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
0赞 vishwas babar 10/4/2023 #2

在 AndroidManifest .xml 文件中,在应用程序标签内替换此行,

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

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

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