菜单项未显示在Android应用中

发布于 2025-02-01 01:49:18 字数 2464 浏览 3 评论 0原文

每个人!我是初学者,开发一个移动应用程序。 朋友,我的菜单项未显示。我单击三个点单击菜单。出现一个白色窗口没有菜单项的窗口程序代码“菜单.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"
    app:theme="@style/Theme.MAGIC">

    <item
        android:title="@string/search"
        android:id="@+id/search_option"
        android:icon="@drawable/ic_baseline_search_24"
        app:showAsAction="ifRoom"
        app:actionViewClass="androidx.appcompat.widget.SearchView">
    </item>

    <item
        android:id="@+id/setting_app"
        android:title="@string/menu_settings"
        android:icon="@drawable/ic_settings_app" />

    <item
        android:id="@+id/about_app"
        android:title="@string/menu_about_application"
        android:icon="@drawable/ic_info_about" />
</menu>

程序代码“ themes.xml”:

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="Theme.MAGIC" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    </style>
</resources>

程序代码“ mainActivity.java”

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem;
        androidx.appcompat.widget.SearchView searchView;

        getMenuInflater().inflate(R.menu.menu, menu);
        menuItem = menu.findItem(R.id.search_option);

        searchView = (SearchView) menuItem.getActionView();

        searchView.setOnQueryTextListener(this);
        return super.onCreateOptionsMenu(menu);
    }

如何显示菜单项目?朋友,帮助解决问题! 我正在移动设备上测试该应用程序。

everyone! I'm a beginner, developing a mobile app.
Friends, my menu items are not displayed. I click on three dots clicking on the menu. A white window appears window without menu items.
Program code "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"
    app:theme="@style/Theme.MAGIC">

    <item
        android:title="@string/search"
        android:id="@+id/search_option"
        android:icon="@drawable/ic_baseline_search_24"
        app:showAsAction="ifRoom"
        app:actionViewClass="androidx.appcompat.widget.SearchView">
    </item>

    <item
        android:id="@+id/setting_app"
        android:title="@string/menu_settings"
        android:icon="@drawable/ic_settings_app" />

    <item
        android:id="@+id/about_app"
        android:title="@string/menu_about_application"
        android:icon="@drawable/ic_info_about" />
</menu>

Program code "themes.xml":

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="Theme.MAGIC" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    </style>
</resources>

Program code "MainActivity.java"

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem;
        androidx.appcompat.widget.SearchView searchView;

        getMenuInflater().inflate(R.menu.menu, menu);
        menuItem = menu.findItem(R.id.search_option);

        searchView = (SearchView) menuItem.getActionView();

        searchView.setOnQueryTextListener(this);
        return super.onCreateOptionsMenu(menu);
    }

How do I display the menu items? Friends, help solve the problem!
I'm testing the app on a mobile device.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再见回来 2025-02-08 01:49:18

当我创建菜单时,在increateoptionsmenu覆盖方法中我返回true而不是super

另外,您是否覆盖onoptionsitemselected处理点击的方法?您没有发布该代码。

这是Google创建菜单的文档:
https://develoveling.com/guide.com/guide/topics/topics/ui/menus

When I create my menus, in the onCreateOptionsMenu override method I return true rather than the super.

Also, do you override the onOptionsItemSelected method to handle the clicks? You didn't post that code.

Here is Google's documentation on creating menus:
https://developer.android.com/guide/topics/ui/menus

池予 2025-02-08 01:49:18

问题在XML文件中。添加了以下行:&lt; item name =“ android:textColor”&gt;@color/purple_200&lt;/item quat&gt;。应用菜单看起来像这样:菜单

完整程序代码:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- The basic theme of the application -->
<style name="Theme.MAGIC" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- another part of the code -->

    <!-- Text color, including menu text -->
    <item name="android:textColor">@color/purple_200</item>
</style>

The problem was in the XML file. Added the following line: <item name="android:textColor">@color/purple_200</item>. The application menu looks like this:menu

Full program code:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- The basic theme of the application -->
<style name="Theme.MAGIC" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- another part of the code -->

    <!-- Text color, including menu text -->
    <item name="android:textColor">@color/purple_200</item>
</style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文