如何更改工具栏导航和溢出菜单图标 (appcompat v7)? [英] How to change Toolbar Navigation and Overflow Menu icons (appcompat v7)?
问题描述
我在使用 v7 工具栏时遇到了困难.ActionBar
曾经是一个简单的任务,现在看起来过于复杂.无论我设置什么样式,我都无法更改导航图标(打开抽屉)或溢出菜单图标(打开菜单).
I am having a hard time with v7 Toolbar. What was once a simple task for ActionBar
, now seems overly complex. No matter what style I set, I cannot change either navigation icon (which opens a Drawer) or overflow menu icon (which opens a menu).
所以我有一个工具栏
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ghex"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Light"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>
我的代码看起来像这样
//before in the code I do
mToolbar = (Toolbar) findViewById(R.id.toolbar);
private void initToolbar() {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
现在,我需要更改这两个图标的 Drawable
.
Now, I need to change the Drawable
for those two icons.
我如何为 compat v7 Toolbar
执行此操作?我想我需要更改抽屉打开时可见的箭头(Android 5.0).
How do I do this for compat v7 Toolbar
? I guess I would need to change the arrow visible when the drawer is open (Android 5.0).
推荐答案
要更改导航图标,您可以使用:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.my_icon);
要更改溢出图标,您可以使用以下方法:
To change the overflow icon you can use the method:
toolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.ic_my_menu);
如果您想更改图标的颜色,您可以使用:
If you would like to change the color of the icons you can use:
带有 Material Components 主题(例如带有 MaterialToolbar
):
with a Material Components Theme (with a MaterialToolbar
for example):
<com.google.android.material.appbar.MaterialToolbar
android:theme="@style/MyThemeOverlay_Toolbar"
...>
<style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- color used by navigation icon and overflow icon -->
<item name="colorOnPrimary">@color/myColor</item>
</style>
<小时>
使用 AppCompat 主题:
<android.support.v7.widget.Toolbar
app:theme="@style/ThemeToolbar" />
<style name="ThemeToolbar" parent="Theme.AppCompat.Light">
<!-- navigation icon color -->
<item name="colorControlNormal">@color/my_color</item>
<!-- color of the menu overflow icon -->
<item name="android:textColorSecondary">@color/my_color</item>
</style>
<小时>
您还可以更改覆盖应用主题中 actionOverflowButtonStyle
属性的溢出图标:
You can also change the overflow icon overriding in the app theme the actionOverflowButtonStyle
attribute:
具有 Material Components 主题:
With a Material Components Theme:
<style name="AppTheme.Base" parent="Theme.MaterialComponents.DayNight">
<item name="actionOverflowButtonStyle">@style/OverFlow</item>
</style>
<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
<item name="srcCompat">@drawable/my_overflow_menu</item>
</style>
使用 AppCompat 主题:
With an AppCompat theme:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="actionOverflowButtonStyle">@style/OverFlow</item>
</style>
<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
<item name="srcCompat">@drawable/my_overflow_menu</item>
</style>
这篇关于如何更改工具栏导航和溢出菜单图标 (appcompat v7)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!