我已经将我的SDK更新到API 21,现在备份/向上图标是一个指向左边的黑色箭头。
我希望它是灰色的。我该怎么做呢?
例如,在Play Store中,箭头是白色的。
我这样做是为了设置一些样式。我已经使用@drawable/abc_ic_ab_back_mtrl_am_alpha作为homeAsUpIndicator。该可绘制对象是透明的(只有alpha),但箭头显示为黑色。我想知道我是否可以像在DrawerArrowStyle中那样设置颜色。或者如果唯一的解决方案是创建我的@drawable/grey_arrow并将其用于homeAsUpIndicator。
<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
<item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
<item name="android:homeAsUpIndicator" tools:ignore="NewApi">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
</style>
<!-- ActionBar style -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="android:background">@color/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">@color/actionbar_background</item>
</style>
<!-- Style for the navigation drawer icon -->
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@color/actionbar_text</item>
</style>
到目前为止,我的解决方案是使用@drawable/abc_ic_ab_back_mtrl_am_alpha,它看起来是白色的,并使用照片编辑器将其涂成我想要的颜色。它的工作,虽然我更喜欢使用@color/actionbar_text像在DrawerArrowStyle。
最简单(也是最好)的方法来改变背面/向上箭头的颜色。最棒的是
没有副作用(不像其他答案)!
用父控件Widget.AppCompat创建一个样式。DrawerArrowToggle,定义颜色和其他你想要的属性。
在app Theme的drawerArrowStyle属性上设置样式。
创建样式:
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<!-- Set that the nav buttons will animate-->
<item name="spinBars">true</item>
<!-- Set the color of the up arrow / hamburger button-->
<item name="color">@color/white</item>
</style>
在App Theme中设置样式:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- Change global up-arrow color with no side-effects -->
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
要更改工具栏的文本颜色(和其他属性),请创建以下样式:
<style name="ToolbarStyle" parent="Widget.AppCompat.Toolbar">
<item name="android:textColor">@color/white</item>
<item name="titleTextColor">@color/white</item>
<item name="colorControlNormal">@color/white</item> <!-- colorControlNormal is Probably not necessary -->
</style>
然后在AppTheme上设置该样式:
<!-- Base application theme. -->
<style name="AppTheme.MyProduceApp" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- Style the toolbar (mostly the color of the text) -->
<item name="toolbarStyle">@style/ToolbarStyle</item>
<!-- Change color of up-arrow -->
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
这对我来说很管用:
将以下属性添加到主题中。
更改api 21以上的工具栏/动作栏后退箭头
<item name="android:homeAsUpIndicator">@drawable/your_drawable_icon</item>
更改api 21以下的工具栏/动作栏后退箭头
<item name="homeAsUpIndicator">@drawable/your_drawable_icon</item>
更改反向箭头的操作模式
<item name="actionModeCloseDrawable">@drawable/your_drawable_icon</item>
删除工具栏/动作栏阴影
<item name="android:elevation" tools:targetApi="lollipop">0dp</item>
<item name="elevation">0dp</item>
<!--backward compatibility-->
<item name="android:windowContentOverlay">@null</item>
更改动作栏溢出可绘制
<!--under theme-->
<item name="actionOverflowButtonStyle">@style/MenuOverflowStyle</item>
<style name="MenuOverflowStyle" parent="Widget.AppCompat.ActionButton.Overflow">
<item name="srcCompat">@drawable/ic_menu_overflow</item>
注意:使用'srcCompat'而不是'android:src',因为对低于21的api支持向量绘制
查看工具栏和TintManager源代码,drawable/abc_ic_ab_back_mtrl_am_alpha是用样式属性colorControlNormal的值着色的。
我确实尝试在我的项目中设置这个(与<item name="colorControlNormal">@color/my_awesome_color</item>在我的主题),但它仍然是黑色的。
更新:
找到了。你需要用colorControlNormal设置actionBarTheme属性(不是actionBarStyle)。
Eg:
<style name="MyTheme" parent="Theme.AppCompat.Light">
<item name="actionBarTheme">@style/MyApp.ActionBarTheme</item>
<item name="actionBarStyle">@style/MyApp.ActionBar</item>
<!-- color for widget theming, eg EditText. Doesn't effect ActionBar. -->
<item name="colorControlNormal">@color/my_awesome_color</item>
<!-- The animated arrow style -->
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<style name="MyApp.ActionBarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<!-- THIS is where you can color the arrow! -->
<item name="colorControlNormal">@color/my_awesome_color</item>
</style>
<style name="MyApp.ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar">
<item name="elevation">0dp</item>
<!-- style for actionBar title -->
<item name="titleTextStyle">@style/ActionBarTitleText</item>
<!-- style for actionBar subtitle -->
<item name="subtitleTextStyle">@style/ActionBarSubtitleText</item>
<!--
the actionBarTheme doesn't use the colorControlNormal attribute
<item name="colorControlNormal">@color/my_awesome_color</item>
-->
</style>
试试这个
public void enableActionBarHomeButton(AppCompatActivity appCompatActivity, int colorId){
final Drawable upArrow = ContextCompat.getDrawable(appCompatActivity, R.drawable.abc_ic_ab_back_material);
upArrow.setColorFilter(ContextCompat.getColor(appCompatActivity, colorId), PorterDuff.Mode.SRC_ATOP);
android.support.v7.app.ActionBar mActionBar = appCompatActivity.getSupportActionBar();
mActionBar.setHomeAsUpIndicator(upArrow);
mActionBar.setHomeButtonEnabled(true);
mActionBar.setDisplayHomeAsUpEnabled(true);
}
函数调用:
enableActionBarHomeButton(this, R.color.white);