จะเปลี่ยนสีของปุ่มย้อนกลับของ Toolbar ใน Android ได้อย่างไร?


100

ฉันไม่สามารถเปลี่ยนสีของปุ่มย้อนกลับได้ ฉันใช้การออกแบบวัสดุแถบเครื่องมือ ในแอปของฉันฉันใช้พื้นหลังสีดำของแถบเครื่องมือ แต่การออกแบบด้านหลังเป็นสีดำตามค่าเริ่มต้นนั่นเป็นเหตุผลว่าทำไมฉันถึงแค่ต้องการเปลี่ยนสีของปุ่มย้อนกลับ โปรดให้คำตอบแก่ฉัน

ขอบคุณ


3
<style name = "MyMaterialTheme.Base" parent = "Theme.AppCompat.Light.DarkActionBar">
Nazima Kauser MMF

คำตอบ:


79

ใช้สไตล์นี้

<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
    <item name="android:homeAsUpIndicator">@drawable/back_button_image</item>
</style>

ฉันใช้สไตล์นี้ สิ่งที่ฉันต้องเปลี่ยนในรูปแบบนี้?
Nazima Kauser MMF

คุณสามารถใช้ไฟล์ภาพที่กำหนดเองสำหรับปุ่มย้อนกลับได้เพียงเรียกมันใน `` @ drawable / your_image '
Akhil Jayakumar

วิธีนี้เป็นวิธีที่ดีที่สุด เนื่องจากการใช้ R.drawable.abc_ic_ab_back_mtrl_am_alpha เพื่อเปลี่ยนสีมีความเสี่ยงเนื่องจากเวอร์ชันสนับสนุนจะเปลี่ยนทรัพยากรที่เบิกได้บ่อย
Mostafa Imran

196

คุณสามารถเพิ่มสไตล์ให้กับ 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>

และเพิ่มสิ่งนี้เป็นธีมในแถบเครื่องมือของคุณในแถบเครื่องมือ layout.xml โดยใช้app: themeตรวจสอบด้านล่าง

<?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>

4
ทำงานอย่างมีเสน่ห์และฟังดูเหมาะสมที่สุด
Vijay Kumar Kanta

1
ยี้. มันได้ผล. ตอนนี้ฉันต้องการสิ่งที่เทียบเท่ากับ iOS ขอบคุณ!
Ernesto

จะดีกว่าถ้าเรียกว่า ToolbarTheme
Leo Droidcoder

1
สำหรับฉัน <style name = "ToolbarTheme" parent = "@ style / Theme.AppCompat.NoActionBar"> ใช้งานได้ ธีมแอปหลักของฉันคือ <style name = "AppTheme" parent = "Theme.AppCompat.NoActionBar">
ยาร์

3
มันได้ผล สิ่งนี้เหมาะสมกว่าคำตอบที่ยอมรับ
Domenico

86

นี่คือวิธีที่ง่ายที่สุดในการบรรลุ Light and Dark Theme สำหรับ Toolbar คุณต้องเปลี่ยนค่าของapp:themeแท็ก Toolbar

  • สำหรับBlack Toolbar TitleและBlack Up arrowแถบเครื่องมือของคุณควรใช้ธีมต่อไปนี้:

แอป: theme = "@ style / ThemeOverlay.AppCompat.Light"

ชื่อแถบเครื่องมือสีดำและลูกศรขึ้น

  • สำหรับชื่อแถบเครื่องมือสีขาวและลูกศรสีขาวแถบเครื่องมือของคุณควรใช้ชุดรูปแบบต่อไปนี้:

แอป: theme = "@ style / ThemeOverlay.AppCompat"

ชื่อแถบเครื่องมือสีขาวและลูกศรขึ้น


66
สำหรับหัวข้อ Toolbar สีขาวและลูกศรขึ้นสีขาวให้ใช้ธีมต่อไปนี้: android: theme = "@ style / ThemeOverlay.AppCompat.Dark.ActionBar"
Amol Patil

ฉันพยายามทำความเข้าใจทำไมมันถึงชื่อ ThemeOverlay เหตุใดฉันจึงใช้ Theme.AppCompat.Dark.ActionBar แบบธรรมดาไม่ได้
Gaket

@ ch3tanz คุณใช้ ActionBar หรือ Toolbar? ทำไมคุณถึงใช้สไตล์ ActionBar
f470071

@ f470071 ฉันใช้ Toolbar มันเป็นเพียงสไตล์ที่รองรับในทุกเวอร์ชัน คุณสามารถใช้รูปแบบดีไซน์ Material ได้หากแอปของคุณกำหนดเป้าหมายเหนือ Lollipop
ch3tanz

Theme.AppCompat.Light.DarkActionBarในกรณีของฉัน
Evi Song

30

สำหรับชื่อแถบเครื่องมือสีขาวและลูกศรขึ้นสีขาวให้ใช้ธีมต่อไปนี้:

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

ขอบคุณมากเหนือสิ่งอื่นใดการแก้ปัญหานี้ใช้ได้กับฉันเท่านั้น
Md Juyel Rana

20

ใช้สิ่งนี้:

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/white</item> 
</style>

5
สิ่งนี้ควรได้รับการยอมรับ ทุกคำตอบที่นี่เป็นเพียงการแทนที่ไอคอนไม่ใช่สี
Michał Jablonski

12

ลองสิ่งนี้

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);

6

คุณไม่จำเป็นต้องเปลี่ยนสไตล์สำหรับมัน หลังจากตั้งค่าแถบเครื่องมือของคุณเป็นแถบการทำงานคุณสามารถเขียนโค้ดแบบนี้ได้

android.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
android.getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
//here back is your drawable image

แต่คุณไม่สามารถเปลี่ยนสีของลูกศรย้อนกลับด้วยวิธีนี้


6

หากคุณต้องการปุ่มย้อนกลับสีขาว (←) และชื่อแถบเครื่องมือสีขาวให้ทำตามสิ่งนี้:

<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ถ้าคุณต้องการปุ่มย้อนกลับสีดำ (←) และชื่อแถบเครื่องมือสีดำ


5

คุณสามารถใช้ไอคอนของคุณเองโดยใช้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" />

สิ่งนี้ใช้ได้ผลสำหรับฉันเพียงแค่ตั้งค่าไอคอนโดยใช้บรรทัดนี้app:navigationIcon="@drawable/ic_back_black"
Abhishek

มันช่วยได้
Mohamed Nageh

5

เพียงแค่ใช้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>

4

ฉันคิดว่าหากธีมควรสร้างปัญหา แต่ตั้งค่าลูกศรสีดำแบบไดนามิกดังนั้นฉันแนะนำให้ลองอันนี้

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);

2
โซลูชันของคุณแตกต่างจาก @Parth Bhayani อย่างไร?
CoolMind

3

ฉันใช้<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar>ธีมในแอปพลิเคชัน Android ของฉันและในธีม NoActionBar colorControlNormalคุณสมบัติถูกใช้เพื่อเปลี่ยนสีของไอคอนการนำทางเริ่มต้นลูกศรปุ่มย้อนกลับในแถบเครื่องมือของฉัน

styles.xml

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/your_color</item> 
</style>

2

เรียบง่ายและไม่ต้องกังวล

 <?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>

2

ฉันทำวิธีนี้โดยใช้ไลบรารีส่วนประกอบวัสดุ:

<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>

ใช้งานได้: ช่วยให้คุณเปลี่ยนสีของไอคอนเมนูแฮมเบอร์เกอร์
lnaie

1

คุณสามารถเพิ่มรหัสนี้ลงในคลาส java ของคุณ แต่คุณต้องสร้างเนื้อหาเวกเตอร์ก่อนจึงจะปรับแต่งลูกศรย้อนกลับได้

actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);

0

ในการจัดรูปแบบ Toolbar บน 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>

-1

หากคุณต้องการให้ระบบกลับมาเป็นสีขาวคุณสามารถใช้รหัสนี้ได้

<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);
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.