简体   繁体   中英

Android: Toolbar Back Button Is Transparent

I Added a back button to a activity's toolbar but it looks transparent for some reason.

toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_toolbar_back));
    toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.colorTitleText), PorterDuff.Mode.SRC_ATOP);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

Notes: Language is Java.

android.support.v7.widget.Toolbar

android.support.v7.app.AppCompatActivity

Try using ContextCompat() to get color like this below code.

 toolbar.navigationIcon?.setColorFilter(ContextCompat.getColor(this, R.color.white),
                PorterDuff.Mode.SRC_ATOP)

I found out that the problem was not caused by my code, it was caused by Android Studio's weird Image Asset creation. While creating a image asset type toolbar icon Android Studio changes the image's alpha.

My solution is using this site and adding assets manualy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM