如何检测对 ActionBar 标题的点击? [英] How can I detect a click on the ActionBar title?
问题描述
对于特定的客户要求,我需要允许我的应用的用户(不会在 Market 中发布)点击 ActionBar 标题来执行一些操作.
For specific customer requirement, I need to allow the user of my app ( won't be published in Market) to click on the ActionBar title to execute some actions.
我一直在寻找 Android 源代码,但找不到 actionBar TextView 标题的 ID.
I have been looking in the Android source, but I am not able to find an ID for the actionBar TextView title.
是否有适当的方法来处理此类点击?
Is there a proper way to handle such clicks?
推荐答案
标题是不可点击的AFAIK.图标/徽标是可点击的——您可以通过 onOptionsItemSelected()
中的 android.R.id.home
获得它.可以想象,标题也是如此,尽管他们没有提及,我也不会依赖它.
The title is non-clickable AFAIK. The icon/logo is clickable -- you'll get that via android.R.id.home
in onOptionsItemSelected()
. Conceivably, the title also routes this way, though they don't mention it and I wouldn't rely upon it.
听起来您想要一个 Spinner
供用户选择要执行的操作.如果是这样,请使用 setListNavigationCallbacks()
.如果您想删除现在多余的标题,请使用 setDisplayOptions(0, DISPLAY_SHOW_TITLE)
.
It sounds like you want a Spinner
for the user to choose the actions to execute. If so, use setListNavigationCallbacks()
. If you want to remove the title as now being superfluous, use setDisplayOptions(0, DISPLAY_SHOW_TITLE)
.
如果您想要操作栏左侧的 Spinner
以外的其他内容,请使用 setDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM)
和 setCustomView()代码>.请注意,不建议使用这种方法("避免在操作栏"),因为它可能不适用于手机,尤其是在纵向模式下.
If you want something other than a Spinner
on the left side of the action bar, use setDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM)
and setCustomView()
. Note that this approach is not recommended ("Avoid using custom navigation modes in the action bar"), as it may not work well with phones, particularly in portrait mode.
另一种可能性是删除标题并使用徽标代替图标,并在徽标中将您的标题"作为图像的一部分.整个徽标应该是可点击的,通过 onOptionsItemSelected()
拾取.
Another possibility would be to remove the title and use a logo instead of the icon, and in the logo have your "title" as part of the image. The whole logo should be clickable, picked up via onOptionsItemSelected()
.
这篇关于如何检测对 ActionBar 标题的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!