Change logo that appears in Android ActionBar prog

2020-07-16 09:28发布

问题:

Is there a way to change the logo that appears in the ActionBar inside an Activity instead of listing it in the Manifest?

The reason I ask is I have an app for phones where I build a similar type of bar across the top with the launcher icon in the left corner similar to the ActionBar using an ImageView.

I also allow the user to change that image to one on their phone or on the internet. I am currently making the app work for tablets and would like to just use the ActionBar instead but can't figure out how to change the image based on the user. There is no method in the ActionBar class and I could not find a way to modify the icon or logo from the resources.

Anyone know if this is possible or could suggest something I could try?

回答1:

Prior to the introduction in API 14 of the setIcon and setLogo methods that Jake mentions, you appear to be able to change the ActionBar logo by grabbing the view of android.R.id.home and setting a new drawable.

ImageView logo = (ImageView) findViewById(android.R.id.home);
logo.setImageDrawable(drawable);


回答2:

API 14 introduced setIcon and setLogo methods.

As for 11 through 13 there's no easy way.

You could possibly use a custom navigation and hide the regular icon/logo by having your own layout mimic its functionality.

There is a homeLayout attribute in the action bar style which allows specifying an alternate layout resource to be used but it will always throw a ClassCastException since it is instantly cast to an internal class upon inflation (bug #21842).