如何在工具栏中插入搜索视图? [英] how to insert search view in toolbar?
本文介绍了如何在工具栏中插入搜索视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
几行logcat
11-26 01:14:04.752 16902-16902/com.androidbelieve.drawerwithswipetabs W/dalvikvm: VFY: unable to resolve virtual method 3268: Landroid/support/v4/app/Fragment;.performOptionsMenuClosed (Landroid/view/Menu;)V
11-26 01:14:04.752 16902-16902/com.androidbelieve.drawerwithswipetabs D/dalvikvm: VFY: replacing opcode 0x6f at 0x04ac
11-26 01:14:07.315 16902-16902/com.androidbelieve.drawerwithswipetabs D/AbsListView: onDetachedFromWindow
11-26 01:14:08.896 16902-16902/com.androidbelieve.drawerwithswipetabs I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
11-26 01:14:08.936 16902-16902/com.androidbelieve.drawerwithswipetabs D/AbsListView: Get MotionRecognitionManager
11-26 01:14:08.936 16902-16902/com.androidbelieve.drawerwithswipetabs I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
手机上该应用程序的2屏幕截图.
2-Screenshot of the app on my phone.
推荐答案
XML
I-在保存在res/xml目录中的searchable.xml
中创建可搜索的配置.
I - Create a searchable configuration in a searchable.xml
saved in res/xml directory.
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>
II-在AndroidManifest.xml
文件中声明您的可搜索活动.
II - Declare your searchable activity in the AndroidManifest.xml
file.
<activity android:name=".SearchActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
III-在任何layout.xml
<android.support.v7.widget.SearchView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
样式
I.在styles.xml
文件中声明自定义样式.
I. Declare custom styles in your styles.xml
file.
<style name="SearchViewTheme" >
<item name="colorControlActivated">@color/amber500</item>
<item name="colorControlNormal">@color/green500</item>
</style>
II.应用此样式
<android.support.v7.widget.SearchView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:searchIcon="@drawable/ic_library"
app:theme="@style/SearchViewTheme"/>
JAVA
I-使用OnCreate
方法设置SearchView
I- Setting up the SearchView in the OnCreate
method
SearchView searchView = (SearchView) findViewById(R.id.search);
// Sets searchable configuration defined in searchable.xml for this SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
II-接收搜索查询
SearchView searchView = (SearchView) findViewById(R.id.search);
// Sets searchable configuration defined in searchable.xml for this SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
III-聆听用户输入(获取输入)
III- Listening to the user inputs (getting inputs)
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
searchFor(query);
return true;
}
@Override
public boolean onQueryTextChange(String query) {
filterSearchFor(query);
return true;
}
});
这篇关于如何在工具栏中插入搜索视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文