setsupportactionbar删除菜单项

ffx8fchx  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(361)

抱歉,如果我没有解释清楚我的问题。
我有一个利用materialtoolbar的项目,它的外观引用menu.xml。菜单中有我想使用的图标(收藏夹、删除等)。
在我的代码中,但是在初始化时 setSupportActionBar() ,它覆盖了我设置的引用,图标不再出现在导航图标旁边。
我需要 setSupportActionBar() 对于我的项目,有什么方法可以让我保留 setSupportActionBar() 同时保持 top_app_bar.xml ? 或者至少是菜单项中初始化的图标?
活动主菜单.xml

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/topAppBar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:menu="@menu/top_app_bar"
        app:navigationIcon="@drawable/ic_menu_24dp"
        style="@style/Widget.MaterialComponents.Toolbar.Primary"/>
</com.google.android.material.appbar.AppBarLayout>

顶部\u app \u bar.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">
    <item
        android:id="@+id/delete"
        android:icon="@drawable/ic_delete_24dp"
        android:title="test"
        app:showAsAction="ifRoom"
        />
</menu>

<?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">
    <item
        android:id="@+id/delete"
        android:icon="@drawable/ic_delete_24dp"
        android:title="test"
        app:showAsAction="ifRoom"
        />
</menu>

主菜单.java

MaterialToolbar topAppBar = findViewById(R.id.topAppBar);
setSupportActionBar(topAppBar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
wf82jlnq

wf82jlnq1#

这是预期的行为。将动作栏设置为 Activity 它的作用是 Activity 决定要显示的菜单。
这是的文档和方法声明 setSupportActionBarappcompat-1.0.0 附属国。您可以阅读所有内容,但我从javadoc中显式地选择了一个句子,并在下面的片段中提到了它。

/**
 * Set a {@link Toolbar} to act as the {@link ActionBar} for this delegate.
 *
 * <p>When set to a non-null value the {@link #getSupportActionBar()} ()} method will return
 * an {@link ActionBar} object that can be used to control the given toolbar as if it were
 * a traditional window decor action bar. The toolbar's menu will be populated with the
 * Activity's options menu and the navigation button will be wired through the standard
 * {@link android.R.id#home home} menu select action.</p>
 *
 * <p>In order to use a Toolbar within the Activity's window content the application
 * must not request the window feature
 * {@link AppCompatDelegate#FEATURE_SUPPORT_ACTION_BAR FEATURE_SUPPORT_ACTION_BAR}.</p>
 *
 * @param toolbar Toolbar to set as the Activity's action bar, or {@code null} to clear it
 */
public abstract void setSupportActionBar(@Nullable Toolbar toolbar);

重点是这句话:


* ... The toolbar's menu will be populated with the

 * Activity's options menu and the navigation button will be wired through the standard
 * {@link android.R.id#home home} menu select action ...

这意味着你必须重写 onCreateOptionsMenu 方法,以便在活动中为操作栏创建菜单:

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

默认情况下,如果不重写此方法,将填充菜单
... 带有标准系统菜单项。
引用的来源(注意,我删除了 onCreateOptionsMenu 与此问题无关的):

/**
 * Initialize the contents of the Activity's standard options menu.  You
 * should place your menu items in to <var>menu</var>.
 *
 * <p>This is only called once, the first time the options menu is
 * displayed.
 *
 * <p>The default implementation populates the menu with standard system
 * menu items. 
 *
 * @param menu The options menu in which you place your items.
 *
 * @return You must return true for the menu to be displayed;
 *         if you return false it will not be shown.
 */
public boolean onCreateOptionsMenu(Menu menu) {
    if (mParent != null) {
        return mParent.onCreateOptionsMenu(menu);
    }
    return true;
}

相关问题