Change logo that appears in Android ActionBar programatically

自闭症网瘾萝莉.ら 提交于 2019-12-06 03:33:33

问题


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



来源:https://stackoverflow.com/questions/8233855/change-logo-that-appears-in-android-actionbar-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!