我试图在我的应用程序中创建action bar search,但在展开状态下,SearchView没有占用整个操作栏宽度(它仍然显示其他操作)!
那么,如何让SearchView填充完整的ActionBar区域(如在GMAIL应用程序中)?
答案 0 :(得分:22)
以上解决方案均不适合我。设置SearchView的MaxWidth对我有用:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
答案 1 :(得分:11)
搞定了
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu);
MenuItem searchViewItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchViewItem.getActionView();
searchView.setIconifiedByDefault(false);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
searchView.setLayoutParams(params);
searchViewItem.expandActionView();
return true;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MySwipeTabbedActivity" >
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView"
/>
<item android:id="@+id/action_share"
android:title="Share"
android:icon="@android:drawable/ic_menu_share"
android:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
android:title="Settings"
android:icon="@android:drawable/ic_menu_preferences"
android:showAsAction="never" />
</menu>
[可选]避免搜索崩溃:
searchViewItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false; //never collapse
}
});
答案 2 :(得分:2)
@Hiep给出的答案是正确的我按照这些步骤来获得我的解决方案。但我正在使用ActionbarCompact lib,所以它需要做一些更改才能让它工作此解决方案只是修改后的答案Hiep,如果您使用appcompact
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:title="search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
materialdesign:showAsAction="always|collapseActionView"
materialdesign:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
在主类 onCreateOptionsMenu
中 @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
searchView.setLayoutParams(params);
searchView.setIconified(false);
MenuItemCompat.expandActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
避免searchView崩溃:如果你在Appcompact中使用上述方法,它会造成崩溃,所以使用这个解决方案来避免这种情况。
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
/* (non-Javadoc)
* @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionExpand(android.view.MenuItem)
*/
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
/* (non-Javadoc)
* @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionCollapse(android.view.MenuItem)
*/
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
谢谢
答案 3 :(得分:1)
尝试此操作只会将所有其他项目设置为ifRoom
<item
android:id="@+id/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="@string/action_search"
app:showAsAction="always"/>
答案 4 :(得分:0)
是的,我真的来晚了:)
但我想分享另一种替代解决方案。这个想法只是在showAsAction
展开时隐藏分组菜单项。
步骤1:
对菜单项进行分组。请确保搜索视图项中的app:showAsAction="collapseActionView|always"
是<group android:id="@+id/myMenuGroup">
<item
android:id="@+id/actionSearch"
android:icon="@drawable/ic_search"
android:title="@string/search"
app:actionViewClass="android.widget.SearchView"
app:iconTint="@color/textColorVariant2"
app:showAsAction="collapseActionView|always" />
<item
android:id="@+id/actionFilter"
android:icon="@drawable/ic_filter"
android:orderInCategory="0"
android:title="@string/filter"
app:iconTint="@color/textColorVariant2"
app:showAsAction="ifRoom" />
..... </group>
onPrepareOptionsMenu()
步骤2:在override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
super.onPrepareOptionsMenu(menu)
this.menu = menu // this.menu is a global scoped variable
return true
}
回调中获取菜单引用
MenuItem.OnActionExpandListener
第3步
将onMenuItemActionExpand()
添加到您的活动中,并按如下所示实施onMenuItemActionExpand()
和override fun onMenuItemActionExpand(p0: MenuItem?): Boolean {
menu?.setGroupVisible(R.id.myMenuGroup, false)
return true
}
override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean {
menu?.setGroupVisible(R.id.myMenuGroup, true)
return true
}
SearchView
即使存在其他ActionBar
图标,这种方法也会将menu
完全扩展到Yes
内部