![](/img/trans.png)
[英]How to change the Text color of collapsing toolbar and the back button color of collapsing toolbar in android
[英]How to change color of Toolbar back button in Android?
我無法更改后退按鈕的顏色。 我正在使用工具欄材料設計。 在我的應用程序中,我應用了工具欄的黑色背景,但默認情況下背面設計為黑色,這就是為什么我只想更改此后退按鈕的顏色。 請給我解決方案。
謝謝你
您可以在styles.xml 中添加樣式,
<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<!-- Customize color of navigation drawer icon and back arrow -->
<item name="colorControlNormal">@color/toolbar_color_control_normal</item>
</style>
並使用app:theme將此作為主題添加到您的工具欄 layout.xml 中的工具欄,檢查下面
<?xml version="1.0" encoding="utf-8"?>
<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:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ToolbarTheme" >
</android.support.v7.widget.Toolbar>
使用這種風格
<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">@drawable/back_button_image</item>
</style>
對於白色工具欄標題和白色向上箭頭,請使用以下主題:
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
用這個:
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">@color/white</item>
</style>
嘗試了以上所有建議。 我設法在工具欄中更改導航圖標默認后退按鈕箭頭顏色的唯一方法是在基本主題中設置colorControlNormal
,如下所示。 可能是因為父級正在使用Theme.AppCompat.Light.NoActionBar
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">@color/white</item>
//the rest of your codes...
</style>
嘗試這個,
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
只需使用MaterialToolbar
並覆蓋默認顏色:
<com.google.android.material.appbar.MaterialToolbar
style="@style/Widget.MaterialComponents.Toolbar.Primary"
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/...</item>
</style>
如果您使用androidx.appcompat.widget.Toolbar
或MaterialToolbar
與默認樣式( Widget.MaterialComponents.Toolbar
),您可以使用:
<androidx.appcompat.widget.Toolbar
android:theme="@style/MyThemeOverlay_Toolbar2"
和:
<style name="MyThemeOverlay_Toolbar2" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- This attributes is used by title -->
<item name="android:textColorPrimary">@color/white</item>
<!-- This attributes is used by navigation icon and overflow icon -->
<item name="colorOnPrimary">@color/secondaryColor</item>
</style>
你不必為它改變風格。 將您的工具欄設置為操作欄后,您可以這樣編碼
android.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
android.getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
//here back is your drawable image
但是你不能通過這種方法改變后箭頭的顏色
您可以使用app:navigationIcon
和標題顏色app:titleTextColor
使用自己的圖標
例子:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"
app:navigationIcon="@drawable/ic_arrow_back_white_24dp"
app:titleTextColor="@color/white" />
我認為如果主題應該產生一些問題,但它動態設置的黑色箭頭。所以我建議嘗試這個。
Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
backArrow.setColorFilter(getResources().getColor(R.color.md_grey_900), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(backArrow);
如果你想要白色的后退按鈕(←)和白色的工具欄標題,請按照以下步驟操作:
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
如果您想要黑色后退按鈕 (←) 和黑色工具欄標題,請將主題從Dark
更改為Light
。
在 Android 21+ 上設置工具欄的樣式有點不同。
<style name="DarkTheme.v21" parent="DarkTheme.v19">
<!-- toolbar background color -->
<item name="android:navigationBarColor">@color/color_primary_blue_dark</item>
<!-- toolbar back button color -->
<item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation.Tinted</item>
</style>
<style name="Toolbar.Button.Navigation.Tinted" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="tint">@color/color_white</item>
</style>
我使用 Material Components 庫這樣做:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
</style>
<style name="AppTheme.DrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">@android:color/white</item>
</style>
我在我的 android 應用程序和 NoActionBar 主題中使用<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar>
主題, colorControlNormal
屬性用於更改工具欄中導航圖標默認后退按鈕箭頭的顏色
樣式文件
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">@color/your_color</item>
</style>
您可以將此代碼添加到您的 java 類中。 但是您必須先創建一個矢量資產,以便您可以自定義返回箭頭。
actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);
簡單無后顧之憂
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="@color/white"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/testing" />
</android.support.design.widget.CoordinatorLayout>
我們可以通過以下代碼以編程方式實現。 無需像許多其他人建議的那樣添加我們自己的后退圖標。
private void initToolBar(String title) {
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(title);
toolbar.setTitleTextColor(getResources().getColor(R.color.gold));
setSupportActionBar(toolbar);
// add back arrow to toolbar
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Drawable nav = toolbar.getNavigationIcon();
if(nav != null) {
nav.setTint(getResources().getColor(R.color.gold));
}
}
}
如果在getSupportActionBar().setDisplayHomeAsUpEnabled(true);
之前請求,請注意toolbar.getNavigationIcon()
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
它不會工作。
對於白色工具欄標題和白色向上箭頭,請使用以下主題:
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
我正在使用navigationIconTint
屬性用於更改工具欄中導航圖標默認后退按鈕箭頭的顏色
styles.xml
<style name="AppTheme.ToolbarStyle" parent="Widget.MaterialComponents.Toolbar.Surface">
<item name="titleCentered">true</item>
<item name="titleTextColor">?attr/colorOnSurface</item>
<item name="navigationIconTint">?attr/colorOnSurface</item>
</style>
如果您希望系統背景顏色為白色,則可以使用此代碼
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_notification"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<include layout="@layout/action_bar_notification" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
注意:- android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
是關鍵
粘貼在您要在操作欄上顯示后退按鈕的活動中
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_notification);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.