用户定义的 ActionBar Action View:获得正确的宽度
关于冰淇淋三明治:
我希望通过标准 AutoCompleteTextView 添加到 ActionBar a href="http://developer.android.com/guide/topics/ui/actionbar.html#ActionView">Action View 机制(因为 SearchView 在 ICS 之前不可用,我也在使用 ActionBarSherlock):
<item android:id="@+id/menu_search" android:actionViewClass="com.example.AutoCompleteActionView" android:showAsAction="ifRoom" android:title="@string/address"></item>
<item android:id="@+id/menu_close" android:icon="@drawable/ic_menu_close_clear_cancel" android:showAsAction="always"></item>
<item android:id="@+id/menu_ok" android:icon="@drawable/ic_menu_ok" android:showAsAction="always"></item>
这是可行的,但是默认情况下它不会消耗 ActionBar 中的可用空间,这是我想要的。
我查看了 SearchView 的源 并了解它如何覆盖 onMeasure,并为我自己的类做了同样的事情派生自 AutoCompleteTextView。当我这样做时,AutoCompleteTextView 会占用所有空间,没有为我想在其右侧显示的两个菜单项留下空间。
看起来好像是从 MeasureSpec.getSize() 当 MeasureSpec.getMode 时,不考虑其他两个菜单项() 是 MeasureSpec.AT_MOST。
有人做过类似的事情吗?有什么建议吗?
谢谢, 达米安
On Ice Cream Sandwich:
I'm looking to add an AutoCompleteTextView to an ActionBar through the standard Action View mechanism (because SearchView isn't available pre-ICS and I'm also using ActionBarSherlock):
<item android:id="@+id/menu_search" android:actionViewClass="com.example.AutoCompleteActionView" android:showAsAction="ifRoom" android:title="@string/address"></item>
<item android:id="@+id/menu_close" android:icon="@drawable/ic_menu_close_clear_cancel" android:showAsAction="always"></item>
<item android:id="@+id/menu_ok" android:icon="@drawable/ic_menu_ok" android:showAsAction="always"></item>
This works, however by default it does not consume the available space in the ActionBar, which I would like.
I've looked at the source for the SearchView and seen how it overrides onMeasure, and done the same thing for my own class that I derived from AutoCompleteTextView. When I do this, the AutoCompleteTextView consumes all the space, leaving no space for two menu items I want to display to the right of it.
It looks as though the width returned from MeasureSpec.getSize() does not take into account the other two menu items when the MeasureSpec.getMode() is MeasureSpec.AT_MOST.
Anyone done anything similar? Any suggestions?
Thanks,
Damian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为这可能会有所帮助:
使用自定义布局创建可折叠菜单项:
这是自定义布局:
然后在您的活动中:
I think this could help:
Create a collapsible menu item with a custom layout:
This is the custom layout:
Then in your activity:
我无法让 Matthias 的建议在代码中工作(也许我遗漏了一些东西),但是在我的 Action View 中添加一个 minWidth 属性到最上面的元素就成功了。
I wasn't able to get Matthias's suggestion to work in code (maybe I was missing something), but adding a minWidth attribute to the topmost element in my Action View did the trick.
这对您有帮助吗?:
This helps you?:
可能晚了好几年,但是如果有人来找你的操作视图,请执行以下操作,将它们设置为默认操作栏图标,
might be light years late, but if someone comes looking do the following on your action views to get them styled as the default action bar icons,
归功于这篇文章: 使 Android ActionBar 的 ActionView 的宽度匹配ActionBar 的宽度。这将使它伸展。
Credit to this post: Make an Android ActionBar's ActionView's Width match the ActionBar's width. This will make it stretch.