修改ToolBar返回按鈕顏色有兩種方式:
1:通過theme修改
這種方式修改可以簡單的在xml文件中使用app:theme = " "屬性
例如:這樣就可以把返回按鈕改成黑色
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
下面這個(gè)可以將返回按鈕改為白色
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
如果黑白色無法滿足要求的話,就需要通過繼承系統(tǒng)的style并覆蓋"colorControlNormal"屬性來實(shí)現(xiàn)
<style name="ToolbarCustomStyle" parent="ThemeOverlay.AppCompat.ActionBar">
<item name="colorControlNormal">@color/white</item>
</style>
然后在ToolBar的app:theme中使用ToolbarCustomStyle就可以了,其中@color/white可以換成任意顏色
2:通過代碼動(dòng)態(tài)修改返回按鈕顏色
方法1中通過xml中使用style的方法不夠靈活,如果需要在使用過程中動(dòng)態(tài)修改顏色可以使用下面的方法
protected void setToolbarCustomTheme() {
Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
if(upArrow != null) {
upArrow.setColorFilter(ContextCompat.getColor(this, R.color.white), PorterDuff.Mode.SRC_ATOP);
if(getSupportActionBar() != null) {
getSupportActionBar().setHomeAsUpIndicator(upArrow);
}
}
}
其中R.color.white可以換成任意顏色