如何在工具栏中插入搜索视图?

时间:2016-11-25 20:15:01

标签: android insert icons toolbar searchview

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 - 手机上应用的屏幕截图。

screenshot of the app on my phoen

1 个答案:

答案 0 :(得分:3)

<强> XML

I - 在res / xml目录中保存的searchable.xml中创建可搜索的配置。

<?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文件中声明您的可搜索活动。

<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

中声明您的SearchView
<android.support.v7.widget.SearchView  
   android:id="@+id/search"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"/>

<强> STYLING

予。在styles.xml文件中声明自定义样式。

<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
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-倾听用户输入(获取输入)

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;
   }
});