问题描述
我现在面临的一些问题,在上下文操作栏搜索查看被称为片段。其中最主要的是,当我搜索查看扩展,它使消失在操作栏中所有其他项目(除了关闭按钮),即使有一些未使用的空间,就像你可以在这个截图中看到:
此外,我也有这个问题的讨论完全一样的问题:
ActionBar总是扩展搜索查看里面图标
这是我的搜索查看的XML code:
<?XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto> <项目机器人:ID =@ + ID / menu_series_cab_search
机器人:图标=@绘制/ ic_search
机器人:标题=@字符串/ ACTION_SEARCH
应用:actionViewClass =android.support.v7.widget.SearchView
应用:showAsAction =ifRoom/>
<项目机器人:ID =@ + ID / action_settings
机器人:标题=@字符串/ action_settings
机器人:orderInCategory =100
应用:showAsAction =从不/>
&所述; /菜单>
这是我的片段的JAVA code实现ActionMode.Callback
@覆盖
公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
。mode.getMenuInflater()膨胀(R.menu.menu_series_cab,菜单);
mSearchView =(搜索查看)MenuItemCompat
.getActionView(menu.findItem(R.id.menu_series_cab_search));
mSearchView.setQueryHint(getResources()的getString(R.string.action_search));
mSearchView.setOnQueryTextListener(新SearchView.OnQueryTextListener(){
@覆盖
公共布尔onQueryTextSubmit(String s)将{
mSearchView.clearFocus();
返回true;
} @覆盖
公共布尔onQueryTextChange(String s)将{
返回false;
}
});
mSearchView.setIconifiedByDefault(假);
返回true;
}@覆盖
在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
mSearchView.requestFocus();
返回true;
}
第一件事就是搜索查看小部件有一个最大固定大小,所以空是inevitabile。
如果你想,当搜索查看崩溃您需要设置应用程序的action_settings项目并没有消失:showAsAction =总是
<?XML版本=1.0编码=UTF-8&GT?;
<项目机器人:ID =@ + ID / menu_series_cab_search
机器人:图标=@绘制/ ic_search
机器人:标题=@字符串/ ACTION_SEARCH
应用:actionViewClass =android.support.v7.widget.SearchView
应用:showAsAction =ifRoom | collapseActionView/>
<项目机器人:ID =@ + ID / action_settings
机器人:标题=@字符串/ action_settings
机器人:图标=@绘制/ abc_ic_menu_moreoverflow_mtrl_alpha
应用:showAsAction =总是/>
和您的Java code应该是这样的:
@覆盖
公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){ inflater.inflate(R.menu.main,菜单);
菜单项searchItem = menu.findItem(R.id.action_search);
//获取搜索查看并设置搜索的配置
SearchManager的=(SearchManager的)getActivity()getSystemService(Context.SEARCH_SERVICE)。
搜索查看=(搜索查看) MenuItemCompat.getActionView(searchItem);//MenuItemCompat.getActionView(searchItem);//menu.findItem(R.id.action_search).getActionView();
//假设当前的活动是搜索活动
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity()getComponentName())。);
searchView.setIconifiedByDefault(假); //不要图标化的工具;默认情况下展开 super.onCreateOptionsMenu(菜单,充气器);
}
I'm facing some problems with a SearchView in a Contextual Action Bar called by a Fragment. The major one is that when my SearchView is expanded, it makes disappear all other items in action bar (except the closing button) even if there's some unused space, like you can see in this screenshot:
Furthermore, I'm also having the same exact problem discussed in this question:ActionBar always expanded SearchView with the icon inside
This is the XML code of my SearchView:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_series_cab_search"
android:icon="@drawable/ic_search"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"/>
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
And this is the JAVA code of my Fragment that implements ActionMode.Callback
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.menu_series_cab, menu);
mSearchView = (SearchView) MenuItemCompat
.getActionView(menu.findItem(R.id.menu_series_cab_search));
mSearchView.setQueryHint(getResources().getString(R.string.action_search));
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
mSearchView.clearFocus();
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
mSearchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
mSearchView.requestFocus();
return true;
}
first thing the searchview widget has a maximum fixed size, so empty space are inevitabile.
If you want that your "action_settings" item doesn't disappear when the searchview collapse you need to set app:showAsAction="always"
<?xml version="1.0" encoding="utf-8"?>
<item android:id="@+id/menu_series_cab_search"
android:icon="@drawable/ic_search"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
app:showAsAction="always" />
And your Java code should be like this:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
// Get the SearchView and set the searchable configuration
searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);//MenuItemCompat.getActionView(searchItem);//menu.findItem(R.id.action_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
super.onCreateOptionsMenu(menu, inflater);
}
这篇关于Android的扩展搜索查看使得其他动作条物品消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!