Android 如何更改工具栏导航和溢出菜单图标(appcompat v7)?
我很难使用v7工具栏。曾经是Android 如何更改工具栏导航和溢出菜单图标(appcompat v7)?,android,android-layout,material-design,android-appcompat,android-toolbar,Android,Android Layout,Material Design,Android Appcompat,Android Toolbar,我很难使用v7工具栏。曾经是ActionBar的简单任务,现在似乎过于复杂了。无论设置什么样式,我都无法更改导航图标(打开抽屉)或溢出菜单图标(打开菜单) 所以我有一个工具栏 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" a
ActionBar
的简单任务,现在似乎过于复杂了。无论设置什么样式,我都无法更改导航图标(打开抽屉)或溢出菜单图标(打开菜单)
所以我有一个工具栏
<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"
>
现在,我需要更改这两个图标的Drawable
如何为compat v7
工具栏
执行此操作?我想我需要更改抽屉打开时可见的箭头(Android 5.0)。要更改导航图标,可以使用:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.my_icon);
要更改溢出图标,可以使用以下方法:
toolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.ic_my_menu);
如果要更改图标的颜色,可以使用:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.my_icon);
使用材质组件主题(例如使用材质工具栏
):
使用AppCompat主题:
<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>
@样式/溢出
@可绘制/我的\u溢出\u菜单
上述所有解决方案在API 21或更高版本中对我有效,但在API 19(KitKat)中无效。在早期版本中,对我来说,做一个小小的改变就成功了。注意Widget.Holo
而不是Widget.AppCompat
<style name="OverFlowStyle" parent="@android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:src">@drawable/ic_overflow</item>
</style>
@可提取/集成电路溢出
如果我们只需要更改汉堡包/背面图标的颜色,有一种简单、简单且更好的方法
它更好,因为它只更改所需图标的颜色,而colorControlNormal
和android:textColorSecondary
也可能影响工具栏的其他子视图
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
@样式/抽屉箭头样式
真的
@android:彩色/白色
要显示图标,请使用getSupportActionBar().setIcon(R.xxx.xxx)
就我而言,代码是:-
getSupportActionBar().setIcon(R.mipmap.ic_启动器)代码>对于右菜单,您可以执行以下操作:
public static Drawable setTintDrawable(Drawable drawable, @ColorInt int color) {
drawable.clearColorFilter();
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawable.invalidateSelf();
Drawable wrapDrawable = DrawableCompat.wrap(drawable).mutate();
DrawableCompat.setTint(wrapDrawable, color);
return wrapDrawable;
}
在你的活动中
结果是:
将默认主题添加到此行
<item name="colorControlNormal">@color/my_color</item>
@color/my\u color
如果要将图标更改为向量,请创建一个新图标。
然后在活动中.java
:
Toolbar toolbar = findViewById(R.id.your_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.your_icon);
mToolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.your_icon2));
要更改向量图标的颜色,请转到向量XML文件。。在本例中,它将是your_icon.xml
,它将如下所示:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/Your_Color"
android:pathData="M15.41,7.41L14,6l-6,6 6,6 1.41,-1.41L10.83,12z"/>
编辑:不能使用colors.XML或其他地方的颜色,必须在向量的XML文件中直接贴花颜色。。所以它看起来是这样的:
android:fillColor="#FFF"
如果要更改菜单项图标、箭头图标(后退/后退)和3点图标,可以使用android:tint
<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:tint">@color/your_color</item>
</style>
@颜色/你的颜色
您在活动中使用的主题添加到一行代码下面
白色的
<style name="AppTheme.NoActionBar">
<item name="android:tint">#ffffff</item>
</style>
or
<style name="AppThemeName" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:tint">#ffffff</item>
</style>
#ffffff
或
#ffffff
黑人
<style name="AppTheme.NoActionBar">
<item name="android:tint">#000000</item>
</style>
or
<style name="AppThemeName" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:tint">#000000</item>
</style>
#000000
或
#000000
要更改选项菜单项的颜色,您可以
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.your_menu, menu)
menu?.forEach {
it.icon.setTint(Color.your_color)
}
return true
}
我只知道如何更改这些项目的颜色。我可以在不更改可绘图项的情况下执行此操作吗?我已经从代码中尝试了setNavigationIcon
,但它没有更改图标。我必须在工具栏XML中引用样式吗?或者我只需在清单文件中设置完整应用程序的样式,并按照您的建议重新设置样式?@sandolone您可以更改颜色,而无需更改绘图。刚刚更新了答案我可以使用第一个样式示例在不影响其他工具栏组件的情况下仅更改oveflow菜单颜色吗?在运行时如何更改溢出按钮的颜色?我认为这是最好的答案。因为没有一个与修改主题和样式相关的答案对我有效。如果你使用的是矢量,你可以像他们那样执行android:tint=“YOUR COLOR”
,嘿,Sachin,这非常有效。你能在android文档中提供一些参考资料,以便我能了解更多关于其他小部件主题的信息吗?
android:fillColor="@color/Your_Color"
android:fillColor="#FFF"
<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:tint">@color/your_color</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="android:tint">#ffffff</item>
</style>
or
<style name="AppThemeName" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:tint">#ffffff</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="android:tint">#000000</item>
</style>
or
<style name="AppThemeName" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:tint">#000000</item>
</style>
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.your_menu, menu)
menu?.forEach {
it.icon.setTint(Color.your_color)
}
return true
}