如何在 Android 中展开的 SearchView(显示 10 个最近搜索)下方添加一个按钮(如“清除历史记录”)?
任何人都可以帮助如何在 SearchView 显示的最近搜索建议列表下方的展开的 SearchView (放置在 ActionBar 中)添加一个按钮吗?
由于 SearchView 扩展了 LinearLayout,我尝试在布局中添加一个按钮,但无法将其放置在扩展建议列表下方。
我制作了一个名为“mysearch”的垂直线性布局,其中包含 SearchView 和其下方的按钮。然后我还在 ActionBar 的 android:actionLayout="@layout/mysearch" in
menu.xml` 中引用了它。
我尝试通过使用在我的活动的 onCreateOptionsMenu
中获取对 SearchView 的引用
SearchView searchView =
(SearchView)menu.findItem(R.id.menu_search).getActionView();
当我运行它时,我得到一个 ClassCastException:cannot conversion LinearLayout to SearchView
,可能是因为 getActionView()
需要 SearchView,但这里返回的是 LinearLayout。我不知道如何解决这个问题。
Can anyone please help in how to add a button to the expanded SearchView (placed in ActionBar) below the list of recent search suggestions the SearchView is showing?
Since SearchView extends LinearLayout, I have tried to add a button in the layout, but I am not able to place it below the expanded suggestions list.
I made a vertical linearlayout called 'mysearch' with a SearchView and a button below it. Then I also referred to it in android:actionLayout="@layout/mysearch" in
menu.xml` for ActionBar.
I try to get a reference to SearchView in onCreateOptionsMenu
of my activity by using
SearchView searchView =
(SearchView)menu.findItem(R.id.menu_search).getActionView();
When I run it, I get a ClassCastException:cannot convert LinearLayout to SearchView
, maybe because getActionView()
is expecting SearchView but here a LinearLayout it being returned. I dont know how to work around this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够将
SearchView
放入垂直LinearLayout
中,然后只需在其下方添加按钮即可。You should be able to put a
SearchView
into a verticalLinearLayout
and simply add the button below it.