【发布时间】:2017-11-06 06:23:52
【问题描述】:
早安先生
我能够重现 WhatsApp 搜索栏,如下图所示:
为此,我创建了两个工具栏和两个菜单,如下所示:
menu_item.xml
<?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"
android:title="@string/action_search"
app:showAsAction="always" />
</menu>
menu_search.xml
<?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_filter_search"
android:icon="@drawable/ic_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always" />
</menu>
activity_main.xml 中的工具栏
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:fitsSystemWindows="true"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
search_toolbar.xml 中的工具栏
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/searchtoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
app:layout_scrollFlags="scroll|enterAlways"
android:background="@color/colorTextPrimary"
android:fitsSystemWindows="true"
app:collapseIcon="@drawable/ic_arrow_back"
app:titleTextColor="@color/colorPrimary"
/>
我实现了 SearchRecentSuggestions 但它不起作用,做一些测试我发现它只有在我直接在 onCreateMenuOptions 中使用 menu_search 中的膨胀时才起作用,但是,我最终失去了图像中显示的动画下面:
使用的代码
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_filter_search).getActionView();
sView = searchView;
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
return super.onCreateOptionsMenu(menu);
}
我尝试在 menu_items 之后为 menu_search 充气,但也没有得到预期的结果。
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_items, menu);
Toolbar searchTool = findViewById(R.id.searchtoolbar);
searchTool.inflateMenu(R.menu.menu_search);
Menu searchM = searchTool.getMenu();
SearchView itemS = (SearchView)searchM.findItem(R.id.action_filter_search).getActionView();
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
sView = itemS;
itemS.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
itemS.setMaxWidth(Integer.MAX_VALUE);
return true;
}
分析一下,我发现它并没有注册我在 SearchView 中输入的内容,因为在我从正常测试返回后,注册了几个单词后,它们与动画一起出现在设计中。
有没有可能我可以在第二个工具栏中扩充两个菜单或启用此注册表并实现此目的?
感谢您的帮助
【问题讨论】:
标签: android xml searchview search-suggestion