Programming

새로운 머티리얼 테마에서 뒤로 화살표의 색을 바꾸는 방법은 무엇입니까?

procodes 2020. 5. 21. 21:35
반응형

새로운 머티리얼 테마에서 뒤로 화살표의 색을 바꾸는 방법은 무엇입니까?


SDK를 API 21로 업데이트했으며 이제 뒤로 / 위로 아이콘이 왼쪽을 가리키는 검은 색 화살표입니다.

검은 뒤로 화살표

회색으로 표시하고 싶습니다. 어떻게해야합니까?

예를 들어 Play 스토어에서 화살표는 흰색입니다.

스타일을 설정하기 위해이 작업을 수행했습니다. 에 사용 @drawable/abc_ic_ab_back_mtrl_am_alpha했습니다 homeAsUpIndicator. 드로어 블은 투명하지만 (알파 만) 화살표는 검은 색으로 표시됩니다. 에서처럼 색상을 설정할 수 있는지 궁금합니다 DrawerArrowStyle. 또는 유일한 해결책은 my를 만들어서 @drawable/grey_arrow사용하는 것입니다 homeAsUpIndicator.

<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light">

    <item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>

    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

    <item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
    <item name="android:homeAsUpIndicator" tools:ignore="NewApi">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
</style>

<!-- ActionBar style -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">

    <item name="android:background">@color/actionbar_background</item>
    <!-- Support library compatibility -->
    <item name="background">@color/actionbar_background</item>
</style>

<!-- Style for the navigation drawer icon -->
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@color/actionbar_text</item>
</style>

내 솔루션은 지금까지 @drawable/abc_ic_ab_back_mtrl_am_alpha흰색 인 것처럼 보이는 사진 편집기를 사용하여 원하는 색상으로 페인트하는 것입니다. @color/actionbar_text에서처럼 사용하는 것이 좋지만 작동합니다 DrawerArrowStyle.


코드를 통해 달성 할 수 있습니다. 뒤로 화살표 드로어 블을 가져 와서 필터로 색상을 수정 한 후 뒤로 버튼으로 설정하십시오.

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

개정 1 :

API 23 (Marshmallow)부터 드로어 블 리소스 abc_ic_ab_back_mtrl_am_alpha가로 변경되었습니다 abc_ic_ab_back_material.

편집하다:

이 코드를 사용하여 원하는 결과를 얻을 수 있습니다.

toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.blue_gray_15), PorterDuff.Mode.SRC_ATOP);

ToolbarTintManager소스를 보면 drawable/abc_ic_ab_back_mtrl_am_alphastyle 속성 값으로 표시 colorControlNormal됩니다.

내 프로젝트에서 ( <item name="colorControlNormal">@color/my_awesome_color</item>내 테마로) 이 설정을 시도 했지만 여전히 검은 색입니다.

업데이트 :

그것을 발견. 당신은 설정해야합니다 actionBarTheme속성 ( 없는 actionBarStyle 과를) colorControlNormal.

예 :

<style name="MyTheme" parent="Theme.AppCompat.Light">        
    <item name="actionBarTheme">@style/MyApp.ActionBarTheme</item>
    <item name="actionBarStyle">@style/MyApp.ActionBar</item>
    <!-- color for widget theming, eg EditText. Doesn't effect ActionBar. -->
    <item name="colorControlNormal">@color/my_awesome_color</item>
    <!-- The animated arrow style -->
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="MyApp.ActionBarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">       
    <!-- THIS is where you can color the arrow! -->
    <item name="colorControlNormal">@color/my_awesome_color</item>
</style>

<style name="MyApp.ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="elevation">0dp</item>      
    <!-- style for actionBar title -->  
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <!-- style for actionBar subtitle -->  
    <item name="subtitleTextStyle">@style/ActionBarSubtitleText</item>

    <!-- 
    the actionBarTheme doesn't use the colorControlNormal attribute
    <item name="colorControlNormal">@color/my_awesome_color</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>

툴바 테마에 단일 속성을 추가해야합니다.

<style name="toolbar_theme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorControlNormal">@color/arrow_color</item>
</style>

이 toolbar_theme를 도구 모음에 적용하십시오.

또는

테마에 직접 적용 할 수 있습니다.

<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/arrow_color</item> 
  //your code ....
</style>

그냥 추가

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

현재 앱 테마로


대부분의 이전 의견에서 언급했듯이 해결책은

<item name="colorControlNormal">@color/white</item>앱 테마로 그러나 레이아웃의 툴바 요소에 다른 테마가 정의되어 있지 않은지 확인하십시오.

     <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

사용 Toolbar하면 이것을 시도 할 수 있습니다

Drawable drawable = toolbar.getNavigationIcon();
drawable.setColorFilter(ContextCompat.getColor(appCompatActivity, colorId), PorterDuff.Mode.SRC_ATOP);

Carles의 대답은 정답이지만 getDrawable (), getColor () 와 같은 메소드 중 일부는 이 답변을 쓸 때 사용되지 않습니다 . 그래서 업데이트 된 답변은

Drawable upArrow = ContextCompat.getDrawable(context, R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(ContextCompat.getColor(context, R.color.white), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

다른 stackoverflow 쿼리에 따라 ContextCompat.getDrawable () 호출 이 다음과 유사 하다는 것을 알았 습니다.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return resources.getDrawable(id, context.getTheme());
} else {
    return resources.getDrawable(id);
}

그리고 ContextCompat.getColor ()

public static final int getColor(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        return ContextCompatApi23.getColor(context, id);
    } else {
        return context.getResources().getColor(id);
    }
}

링크 1 : ContextCompat.getDrawable ()

링크 2 : ContextCompat.getColor ()


롤리팝 이전에 작동하는 솔루션을 찾았습니다. "actionBarWidgetTheme"내에서 "colorControlNormal"을 설정하여 homeAsUpIndicator 색상을 변경하십시오. 위에서와 같이 rockgecko의 답변을 수정하십시오.

<style name="MyTheme" parent="Theme.AppCompat.Light">        
    <item name="actionBarTheme">@style/MyApp.ActionBarTheme</item>
    <item name="actionBarStyle">@style/MyApp.ActionBar</item>
    <!-- color for widget theming, eg EditText. Doesn't effect ActionBar. -->
    <item name="colorControlNormal">@color/my_awesome_color</item>
    <!-- The animated arrow style -->
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <!-- The style for the widgets on the ActionBar. -->
    <item name="actionBarWidgetTheme">@style/WidgetStyle</item>
</style>

<style name="WidgetStyle" parent="style/ThemeOverlay.AppCompat.Light">
    <item name="colorControlNormal">@color/my_awesome_color</item>
</style>

아래 방법을 사용하십시오.

private Drawable getColoredArrow() {
    Drawable arrowDrawable = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    Drawable wrapped = DrawableCompat.wrap(arrowDrawable);

    if (arrowDrawable != null && wrapped != null) {
        // This should avoid tinting all the arrows
        arrowDrawable.mutate();
        DrawableCompat.setTint(wrapped, Color.GRAY);
    }

    return wrapped;
}

이제 다음을 사용하여 드로어 블을 설정할 수 있습니다.

getSupportActionBar().setHomeAsUpIndicator(getColoredArrow());

compileSdkVersoin 25에서 다음을 수행 할 수 있습니다.

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
     <item name="android:textColorSecondary">@color/your_color</item>
</style>

여기에 이미지 설명을 입력하십시오

메뉴 탐색 아이콘 색상 변경

최종 변환 메뉴 아이콘 색상

In style.xml :

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <item name="android:textColor">@color/colorAccent</item>


</style>

<style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@color/colorPrimaryDark</item>
</style>







In Mainfests.xml :

<activity android:name=".MainActivity"
        android:theme="@style/MyMaterialTheme.Base"></activity>

Another solution that might work for you is to not declare your toolbar as the app's action bar ( by setActionBar or setSupportActionBar ) and set the back icon in your onActivityCreated using the code mentioned in another answer on this page

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);
toolbar.setNavigationIcon(upArrow);

Now, you will not get the onOptionItemSelected callback when you press the back button. However, you can register for that using setNavigationOnClickListener. This is what i do:

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        getActivity().onBackPressed(); //or whatever you used to do on your onOptionItemSelected's android.R.id.home callback
    }
});

I'm not sure if it will work if you work with menu items.


try this

public void enableActionBarHomeButton(AppCompatActivity appCompatActivity, int colorId){

    final Drawable upArrow = ContextCompat.getDrawable(appCompatActivity, R.drawable.abc_ic_ab_back_material);
    upArrow.setColorFilter(ContextCompat.getColor(appCompatActivity, colorId), PorterDuff.Mode.SRC_ATOP);

    android.support.v7.app.ActionBar mActionBar = appCompatActivity.getSupportActionBar();
    mActionBar.setHomeAsUpIndicator(upArrow);
    mActionBar.setHomeButtonEnabled(true);
    mActionBar.setDisplayHomeAsUpEnabled(true);
}

function call:

enableActionBarHomeButton(this, R.color.white);

We were running into the same problem and all we wanted was to set the

app:collapseIcon

attribute in the toolbar in the end, which we did not find since it is not very well documented :)

<android.support.v7.widget.Toolbar
         android:id="@+id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="@dimen/toolbarHeight"
         app:collapseIcon="@drawable/collapseBackIcon" />

You can change the color of the navigation icon programmatically like this:

mToolbar.setNavigationIcon(getColoredArrow()); 

private Drawable getColoredArrow() {
        Drawable arrowDrawable = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        Drawable wrapped = DrawableCompat.wrap(arrowDrawable);

        if (arrowDrawable != null && wrapped != null) {
            // This should avoid tinting all the arrows
            arrowDrawable.mutate();
                DrawableCompat.setTintList(wrapped, ColorStateList.valueOf(this.getResources().getColor(R.color.your_color)));
            }
        }

        return wrapped;
}

I just changed the toolbar theme to be @style/ThemeOverlay.AppCompat.Light

and the arrow became dark gray

            <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="center"
            app:layout_collapseMode="pin"
            app:theme="@style/ThemeOverlay.AppCompat.Light">

Simple solution that worked for me (if you need to use the same arrow color all across the app):

  1. Save the arrow icon that you'd like to use to the drawable folder (make sure it has the desired color; you can change your color in xml if you use xml vector drawable).

  2. Assign this new arrow icon to the action bar:

    ActionBar actionBar = getSupportActionBar(); actionBar.setHomeAsUpIndicator(R.drawable.ic_your_icon_here);


Just remove android:homeAsUpIndicator and homeAsUpIndicator from your theme and it will be fine. The color attribute in your DrawerArrowStyle style must be enough.


Unless there's a better solution...

What I did is to take the @drawable/abc_ic_ab_back_mtrl_am_alpha images, which seem to be white, and paint them in the color I desire using a photo editor.

참고 URL : https://stackoverflow.com/questions/26788464/how-to-change-color-of-the-back-arrow-in-the-new-material-theme

반응형