setSupportActionBar 删除菜单项
setSupportActionBar removes menu items
抱歉,如果我没有足够清楚地解释我遇到的问题。
我有一个使用 MaterialToolbar 的项目,它的外观引用了 menu.xml。菜单包含我希望使用的潜在图标(收藏、删除等)。
在我的代码中,但是当初始化 setSupportActionBar()
时,它会覆盖我设置的引用,图标不再出现在导航图标旁边。
我的项目需要 setSupportActionBar()
,有没有一种方法可以在保持 top_app_bar.xml
外观的同时保留 setSupportActionBar()
?或者至少在菜单项中初始化图标?
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
的作用是决定要显示的菜单。
这里是 setSupportActionBar
来自 appcompat-1.0.0
依赖的文档和方法声明。您可以阅读所有内容,但我明确地从 java-doc 中选择了一个句子,并在该片段下方提到了它。
/**
* 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;
}
抱歉,如果我没有足够清楚地解释我遇到的问题。
我有一个使用 MaterialToolbar 的项目,它的外观引用了 menu.xml。菜单包含我希望使用的潜在图标(收藏、删除等)。
在我的代码中,但是当初始化 setSupportActionBar()
时,它会覆盖我设置的引用,图标不再出现在导航图标旁边。
我的项目需要 setSupportActionBar()
,有没有一种方法可以在保持 top_app_bar.xml
外观的同时保留 setSupportActionBar()
?或者至少在菜单项中初始化图标?
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
的作用是决定要显示的菜单。
这里是 setSupportActionBar
来自 appcompat-1.0.0
依赖的文档和方法声明。您可以阅读所有内容,但我明确地从 java-doc 中选择了一个句子,并在该片段下方提到了它。
/**
* 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;
}