我已经将我的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。
这对我来说很管用:
将以下属性添加到主题中。
更改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支持向量绘制
试试这个
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);
查看工具栏和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>