以编程方式更改 Android ActionBar 中显示的徽标
有没有办法更改 Activity
内的 ActionBar
中显示的徽标,而不是在清单中列出它?
我问的原因是我有一个手机应用程序,我在顶部构建了一个类似类型的栏,左上角有启动器图标,类似于使用 ImageView
的 ActionBar
>。
我还允许用户将该图像更改为手机或互联网上的图像。我目前正在使该应用程序适用于平板电脑,并且只想使用 ActionBar
来代替,但无法弄清楚如何根据用户更改图像。 ActionBar 类中没有方法,我找不到从资源中修改图标或徽标的方法。
有人知道这是否可能或可以建议我可以尝试吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Jake 提到的 API 14 中引入
setIcon
和setLogo
方法之前,您似乎可以通过抓取android 的视图来更改 ActionBar 徽标.R.id.home
并设置一个新的可绘制对象。Prior to the introduction in API 14 of the
setIcon
andsetLogo
methods that Jake mentions, you appear to be able to change the ActionBar logo by grabbing the view ofandroid.R.id.home
and setting a new drawable.API 14 引入了
setIcon
和setLogo
方法。至于 11 到 13,没有简单的方法。
您可以使用自定义导航并通过让您自己的布局模仿其功能来隐藏常规图标/徽标。
操作栏样式中有一个
homeLayout
属性,允许指定要使用的备用布局资源,但它总是会抛出ClassCastException
因为它会立即转换为内部类通货膨胀时(bug #21842)。API 14 introduced
setIcon
andsetLogo
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 aClassCastException
since it is instantly cast to an internal class upon inflation (bug #21842).