ฉันไม่สามารถเปลี่ยนสีของปุ่มย้อนกลับได้ ฉันใช้การออกแบบวัสดุแถบเครื่องมือ ในแอปของฉันฉันใช้พื้นหลังสีดำของแถบเครื่องมือ แต่การออกแบบด้านหลังเป็นสีดำตามค่าเริ่มต้นนั่นเป็นเหตุผลว่าทำไมฉันถึงแค่ต้องการเปลี่ยนสีของปุ่มย้อนกลับ โปรดให้คำตอบแก่ฉัน
ขอบคุณ
ฉันไม่สามารถเปลี่ยนสีของปุ่มย้อนกลับได้ ฉันใช้การออกแบบวัสดุแถบเครื่องมือ ในแอปของฉันฉันใช้พื้นหลังสีดำของแถบเครื่องมือ แต่การออกแบบด้านหลังเป็นสีดำตามค่าเริ่มต้นนั่นเป็นเหตุผลว่าทำไมฉันถึงแค่ต้องการเปลี่ยนสีของปุ่มย้อนกลับ โปรดให้คำตอบแก่ฉัน
ขอบคุณ
คำตอบ:
ใช้สไตล์นี้
<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">@drawable/back_button_image</item>
</style>
คุณสามารถเพิ่มสไตล์ให้กับ 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>
นี่คือวิธีที่ง่ายที่สุดในการบรรลุ Light and Dark Theme สำหรับ Toolbar คุณต้องเปลี่ยนค่าของapp:themeแท็ก Toolbar
แอป: theme = "@ style / ThemeOverlay.AppCompat.Light"
แอป: theme = "@ style / ThemeOverlay.AppCompat"
Theme.AppCompat.Light.DarkActionBarในกรณีของฉัน
สำหรับชื่อแถบเครื่องมือสีขาวและลูกศรขึ้นสีขาวให้ใช้ธีมต่อไปนี้:
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
ใช้สิ่งนี้:
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">@color/white</item>
</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);
คุณไม่จำเป็นต้องเปลี่ยนสไตล์สำหรับมัน หลังจากตั้งค่าแถบเครื่องมือของคุณเป็นแถบการทำงานคุณสามารถเขียนโค้ดแบบนี้ได้
android.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
android.getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
//here back is your drawable image
แต่คุณไม่สามารถเปลี่ยนสีของลูกศรย้อนกลับด้วยวิธีนี้
หากคุณต้องการปุ่มย้อนกลับสีขาว (←) และชื่อแถบเครื่องมือสีขาวให้ทำตามสิ่งนี้:
<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ถ้าคุณต้องการปุ่มย้อนกลับสีดำ (←) และชื่อแถบเครื่องมือสีดำ
คุณสามารถใช้ไอคอนของคุณเองโดยใช้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"
เพียงแค่ใช้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>
ฉันคิดว่าหากธีมควรสร้างปัญหา แต่ตั้งค่าลูกศรสีดำแบบไดนามิกดังนั้นฉันแนะนำให้ลองอันนี้
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);
ฉันใช้<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>
เรียบง่ายและไม่ต้องกังวล
<?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>
ฉันทำวิธีนี้โดยใช้ไลบรารีส่วนประกอบวัสดุ:
<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>
คุณสามารถเพิ่มรหัสนี้ลงในคลาส java ของคุณ แต่คุณต้องสร้างเนื้อหาเวกเตอร์ก่อนจึงจะปรับแต่งลูกศรย้อนกลับได้
actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);
ในการจัดรูปแบบ 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>
หากคุณต้องการให้ระบบกลับมาเป็นสีขาวคุณสามารถใช้รหัสนี้ได้
<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);