我希望在操作栏中有一个搜索视图和两个按钮。所以我创建了menu / options_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
<item
android:title="Route"
android:id="@+id/action_route"
android:icon="@drawable/route"
android:showAsAction="always" />
<item
android:title="Cancel"
android:id="@+id/action_cancel"
android:icon="@drawable/cancel"
android:showAsAction="always" />
</menu>
我在代码中充气:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
这就是我得到的:
如您所见,仅显示第一项(&#34;路线&#34;)。 如果我让搜索小部件图标化,我可以看到第二项(&#34;取消&#34;):
//searchView.setIconifiedByDefault(false);
但是,我希望扩展搜索小部件并显示两个项目。有可能吗?
更新
我设法通过{&#34;路线&#34;设置android:orderInCategory="1"
使其可见。和&#34;取消&#34;和android:orderInCategory="2"
搜索小部件:
这几乎是我想要的。问题是我希望搜索小部件首先(最左边),然后是这两个项目。有可能吗?
更新2
我试过&#34;推荐&#34;办法。我设置为SearchView
:
android:showAsAction="always|collapseActionView"
点击搜索图标后,我得到了扩展视图:
两个问题:
SearchView
的可用空间。答案 0 :(得分:2)
创建一个演示,检查它:
menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@android:drawable/ic_search_category_default"
app:actionLayout="@layout/searchview"
app:showAsAction="always|collapseActionView"/>
<item
android:title="Route"
android:id="@+id/action_route"
android:icon="@mipmap/ic_launcher"
app:showAsAction="always" />
<item
android:title="Cancel"
android:id="@+id/action_cancel"
android:icon="@mipmap/ic_launcher"
app:showAsAction="always" />
</menu>
searchview.xml:
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Java:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
if(searchItem!=null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
return true;
}
根据需求编辑Java(始终打开searchview):
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.expandActionView();
final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
if(searchItem!=null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
// This listener use for handle back press if you want to go back without collapsing searchview
MenuItemCompat.setOnActionExpandListener(searchItem,new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
onBackPressed();
return false;
}
});
}
return true;
}
屏幕截图:
答案 1 :(得分:1)
我是通过设置搜索视图的最大宽度,等于屏幕宽度减去操作项宽度来实现的。
<?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/action_search"
android:icon="@drawable/ic_search_white_24dp"
android:title="@string/title_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
<item
android:id="@+id/action_filter"
android:icon="@drawable/ic_filter_list_white_24dp"
android:title="@string/title_filter"
app:showAsAction="always" />
</menu>
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search, menu);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconified(false);
searchView.setMaxWidth(getSearchViewMaxWidth());
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
performSearch("");
return true; // Disable dismissing the search view
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
performSearch(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
@Px
private int getSearchViewMaxWidth() {
final int screenWidth = getResources().getDisplayMetrics().widthPixels;
final int actionItemWidth = getResources().getDimensionPixelSize(R.dimen.action_item_width);
return screenWidth - actionItemWidth;
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- ... -->
<dimen name="action_item_width">48dp</dimen>
</resources>
答案 2 :(得分:0)
按照以下更改onCreateOptionsMenu
,以便在SearchView
菜单项具有焦点时显式设置菜单项。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
if (menu.getItem(0).isFocused()) //detect if search view has focus
{
//explicitly set menu items visible
menu.getItem(1).setVisible(true);
menu.getItem(2).setVisible(true);
}
return true;
}
答案 3 :(得分:0)
您可以通过此
自定义展开的搜索视图的宽度@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT);
params.width=500;
searchView.setLayoutParams(params);
menu.findItem(R.id.action_search).expandActionView();
return true;
}
答案 4 :(得分:0)
试试这个:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:orderInCategory="1"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item
android:title="Route"
android:id="@+id/action_route"
android:icon="@drawable/route"
android:orderInCategory="2"
android:showAsAction="always" />
<item
android:title="Cancel"
android:id="@+id/action_cancel"
android:icon="@drawable/cancel"
android:orderInCategory="2"
android:showAsAction="always" />
</menu>