Android - 工具栏中的SearchView无法展开

时间:2015-05-26 07:07:19

标签: android searchview

我尝试在工具栏中添加SearchView,我已经添加了图标,但是当我按下它时,它没有扩展。

以下是代码:

main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.app.try.MainActivity">
    <item
        android:id="@+id/search"
        android:title="@string/search"
        android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
        app:showAsAction="always"
        android:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

MainActivity.java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.menu_main,menu);

        MenuItem searchItem = menu.findItem(R.id.search);
        SearchManager searchManager= (SearchManager)getSystemService(Context.SEARCH_SERVICE);

        SearchView searchView=null;
        if (searchItem!=null) {
            Log.d("createOptionMenu","search item not null");
            searchView = (SearchView) searchItem.getActionView();
        }

        if (searchView!=null) {
            Log.d("createOptionMenu","search view not null");
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        }else{
            Log.e("createOptionMenu","search view null");
        }



        return true;
    }

(这始终显示日志消息&#34;搜索视图为空&#34;)

的AndroidManifest.xml

...
<activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

        <activity
        android:name=".SearchResultActivity"
        android:label="@string/title_activity_search_result" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
...

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="HINTTT" />

我尝试过使用expandActionView()setIconified(false)的多个选项,但不适用于我。

3 个答案:

答案 0 :(得分:23)

您应该使用MenuItemCompat

mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

用于折叠SearchView使用

MenuItemCompat.collapseActionView(searchItem);

并确保您已将android:actionViewClass更改为app:actionViewClass

答案 1 :(得分:1)

我有同样的问题。我只是通过一个小小的调整来解决我的问题。更改 android:actionViewClass="android.support.v7.widget.SearchView"

app:actionViewClass="android.support.v7.widget.SearchView"

答案 2 :(得分:-2)

在MainActivity中,您必须扩展ActionbarActivity

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">

<item
    android:id="@+id/menu_search"
    android:title="@string/menu_search"
    appcompat:actionViewClass="android.support.v7.widget.SearchView"
    appcompat:showAsAction="always"/>

MainActivity.java

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    // Inflate menu to add items to action bar if it is present.
    inflater.inflate(R.menu.menu_main, menu);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    return true;
}

}