如果我没有足够清楚地解释我遇到的问题,我深表歉意。
我有一个使用 MaterialToolbar 的项目,它引用了 menu.xml 的外观。菜单包含我希望使用的潜在图标( Collection 夹、删除等)。
但是,在我的代码中,当初始化 setSupportActionBar()
时,它会覆盖我设置的引用,图标不再出现在导航图标旁边。
我的项目需要 setSupportActionBar()
,有没有办法可以保留 setSupportActionBar()
,同时保持 top_app_bar.xml
的外观>?或者至少在菜单项中初始化图标?
activity_main_menu.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>
top_app_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>
MainMenu.java
MaterialToolbar topAppBar = findViewById(R.id.topAppBar);
setSupportActionBar(topAppBar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
最佳答案
这是预期的行为。当您将操作栏设置为 Activity
时,Activity
的角色将决定将显示什么菜单。
这里是来自 appcompat-1.0.0
依赖项的 setSupportActionBar
的文档和方法声明。您可以阅读全部内容,但我明确地从 java-doc 中选择了一个句子,并在此 fragment 下面提到了它。
/**
* 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
方法才能在操作栏的 Activity 中创建菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.resource_id, menu);
return true;
}
默认情况下,如果您不重写此方法,则会填充菜单
... with standard system menu items.
引用来源(注意,我从 onCreateOptionsMenu
的 java-doc 中删除了与此问题无关的解释):
/**
* 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;
}
关于java - setSupportActionBar 删除菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63761776/