툴바 홈 아이콘 색상을 변경하는 방법


108

android.support.v7.widget.Toolbar를 사용하고 있으며이 게시물 에서 햄버거 아이콘의 색상을 흰색으로 변경하는 방법을 배웠지 만 전화를 걸면 위쪽 / 뒤로 화살표가 어두운 색으로 유지됩니다.

setDisplayHomeAsUpEnabled(true);

화살표도 흰색으로 만들 수 있습니까?

다음은 setDisplayHomeAsUpEnabled ()를 호출 할 때 내 도구 모음의 모습입니다.

여기에 이미지 설명 입력

... 그리고 다음은 내 styles.xml 파일의 관련 부분입니다.

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">#194C5F</item>
    <item name="colorAccent">@color/accent</item>
    <item name="drawerArrowStyle">@style/WhiteDrawerIconStyle</item>
</style>

    <style name="WhiteDrawerIconStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>

2
흰색 화살표 png 아이콘을 사용하고 검은 색 화살표로 바꿀 수 있습니다.
— Surender Kumar 2015

1
나는 그것을 피하려고 노력하고 있으며 스타일로 색상을 표시합니다 ... 그것이 내가 actionBarDrawerToggle.setHomeAsUpIndicator (R.drawable.arrow);를 통해 할 수 있다는 것을 아는 유일한 방법이라면;
— Joshua W

... 또한 png /com.android.support/appcompat-v7/21.0.3/res/drawable-xxxhdpi/abc_ic_ab_back_mtrl_am_alpha.png가 흰색으로 나타납니다
— Joshua W

@JoshuaWitter 내 문제를 해결해 주셔서 감사합니다. 뒤로 버튼을 클릭하는 것을 감지하도록 도와 주시겠습니까? 여기 내 질문입니다 stackoverflow.com/questions/29138629/...은
— user2056563

1
@JoshuaWitter 흰색 이어도 착색됩니다. 변경 colorControlNormal값
— osrl

답변:


242

styles.xml을 편집하여 해결했습니다.

<style name="ToolbarColoredBackArrow" parent="AppTheme">
    <item name="android:textColorSecondary">INSERT_COLOR_HERE</item>
</style>

... 활동의 도구 모음 정의에서 스타일을 참조합니다.

<LinearLayout
    android:id="@+id/main_parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        app:theme="@style/ToolbarColoredBackArrow"
        app:popupTheme="@style/AppTheme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

13
또한 당신은 설정할 수 있습니다 colorControlNormal대신android:textColorSecondary
— mbelsky

하지만 그것은 전체 위젯의 색상을 바꿀 것입니다. @mbelsky
— 박사 안드로

3
이 API 레벨 21이 필요하다는 @mbelsky 주
— 프란시스코 로메로

Error404 @ : u는 참조로는 API 레벨 1에 추가됩니다 여기에
— 모하메드 하템 압두

안녕하세요, 귀하의 코드를 시도했지만 필요한 결과를 얻지 못했습니다. 내 toolbar.axml 파일에서 설정 테마는 android : theme=@style/ThemeOverlay.AppCompat.Dark.ActionBar이고 내 styles.axml 파일에서 주어진 코드를 작성했지만 탐색 모음 뒤로 버튼 색상은 변경되지 않았습니다.
— Deepak

57

여기에 당신이 찾고있는 것이 있습니다. 그러나 이것은 또한 radioButton 등의 색상을 변경합니다. 따라서 테마를 사용하는 것이 좋습니다.

<item name="colorControlNormal">@color/colorControlNormal</item>

11
이것은 받아 들여진 대답이어야합니다. 이것은 내가 찾고 있던 것을 달성합니다 : <style name = "ActionBarTheme"parent = "ThemeOverlay.AppCompat.Dark.ActionBar"> <item name = "colorControlNormal"> ### COLOR ### </ item> </ style> 툴바에 직접 테마를 적용합니다.
— Mavamaarten

1
부모 속성은 매우 중요합니다.
— ataravati 19

36

이 코드를 사용하여 프로그래밍 방식으로 해결했습니다.

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

개정 1 :

API 23 (Marshmallow)부터 드로어 블 리소스 abc_ic_ab_back_mtrl_am_alpha가 abc_ic_ab_back_material.


5
이것도 설정하십시오. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
— Ishan Fernando

이 작동 Fragment합니까? 그렇다면이 코드를 어디에 넣을까요? OnCreateView방법은 클래스의 루트, 또는 ...?
— AndroidDevBro

1
@AndroidDevBro 예, OnCreate 함수에 넣고 Nouman Tahir가 개정판에 작성한대로 리소스를 업데이트하는 것을 잊지 마십시오 (R.drawable.abc_ic_ab_back_mtrl_am_alpha 대신 R.drawable.abc_ic_ab_back_material 사용) 다음과 같이 SupportActionBar를 가져옵니다. ((AppCompatActivity) getActivity ()). getSupportActionBar (). setHomeAsUpIndicator (upArrow);
— PayToPwn

14

이 대답은 너무 늦었을 수도 있지만 여기에 내가하는 방법이 있습니다. 도구 모음에 스타일을 지정하면 트릭을 수행 할 수 있습니다. 다음 코드로 toolbar.xml을 만듭니다.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_alignParentTop="true"
android:layout_gravity="bottom"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

및 styles.xml

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!--
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

마지막으로 레이아웃 안에 툴바를 포함합니다.

<include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

1
그러나 AppTheme는 어디에서나 참조되지 않습니다. 오타입니까?
— Henry

11

툴바 테마를 ThemeOverlay.AppCompat.Dark로 변경하십시오.

<?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:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    app:theme="@style/ThemeOverlay.AppCompat.Dark">

</android.support.v7.widget.Toolbar>

활동에서 설정

mToolbar = (Toolbar) findViewById(R.id.navigation);
setSupportActionBar(mToolbar);

1
내가 다른 답변의 놀랐습니다, 나는 항상이 방법을 사용
— C. Skjerdal

9
   <!-- ToolBar -->
    <style name="ToolBarTheme.ToolBarStyle"    parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textColorPrimaryInverse">@color/white</item>
    </style>

게시하기에 너무 늦었 기 때문에 뒤로 버튼의 색상을 변경했습니다.


8

이 작업을 수행하는 더 쉬운 방법이 있습니다.

drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.open_drawer, R.string.close_drawer);
arrow = drawerToggle.getDrawerArrowDrawable();

그리고

arrow.setColor(getResources().getColor(R.color.blue);

7

스타일 변경 대신이 두 줄의 코드를 활동에 넣으십시오.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrowleft);

6

내 해결책은 다음과 같습니다.

toolbar.navigationIcon?.mutate()?.let {
  it.setTint(theColor)
  toolbar.navigationIcon = it
}

또는 좋은 기능을 사용하고 싶다면 :

fun Toolbar.setNavigationIconColor(@ColorInt color: Int) = navigationIcon?.mutate()?.let {
    it.setTint(color)
    this.navigationIcon = it
}

용법:

toolbar.setNavitationIconColor(someColor)

이것은 최고의 답변이며 내 문제를 해결합니다
— Zaraki596

5

이 코드는 나를 위해 작동합니다.

public static Drawable changeBackArrowColor(Context context, int color) {
    String resName;
    int res;

    resName = Build.VERSION.SDK_INT >= 23 ? "abc_ic_ab_back_material" : "abc_ic_ab_back_mtrl_am_alpha";
    res = context.getResources().getIdentifier(resName, "drawable", context.getPackageName());

    final Drawable upArrow = context.getResources().getDrawable(res);
    upArrow.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);

    return upArrow;
}

...

getSupportActionBar().setHomeAsUpIndicator(changeBackArrowColor(this, Color.rgb(50, 50, 50)));               
supportInvalidateOptionsMenu();

또한 도구 모음 텍스트 색상을 변경하려는 경우 :

Spannable spannableString = new SpannableString(t);
spannableString.setSpan(new ForegroundColorSpan(Color.rgb(50, 50, 50)), 0, t.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar.setText(spannableString);

API 19에서 25까지 작업.


0

이전 드로어 블 ID " abc_ic_ab_back_material "을 사용하는 대신 모든 API 버전에서 새로운 abc_ic_ab_back_material 을 사용하십시오 . 19, 21, 27에서 테스트했으며 아래 코드와 구성으로 잘 작동합니다.

  • minSdkVersion = 17
  • targetSdkVersion = 26
  • compileSdkVersion = 27

    public static Drawable changeBackArrowColor(Context context, int color) {
    int res;
    res = context.getResources().getIdentifier("abc_ic_ab_back_material", "drawable", context.getPackageName());
    if (res == 0)
        return null;
    final Drawable upArrow = ContextCompat.getDrawable(context, res);
    upArrow.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_ATOP);
    
    return upArrow;

    }


당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.