在 ActionBar 中显示 SearchView Android

Display SearchView in ActionBar Android

我正在尝试在 activity 的 ActionBar 中显示 SearchView。我在此处遵循 android 开发人员指南 here 但是没有显示 SearchView ,我不明白为什么。 这是我的 activity

的代码
    public class SearchActivity extends ActionBarActivity {


    private String query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        FacebookSdk.sdkInitialize(getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_activity_layout);


        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            this.query = intent.getStringExtra(SearchManager.QUERY);
            Toast.makeText(this,query, Toast.LENGTH_LONG);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        return true;
    }

}

这里是 serachable.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="@string/search_hint" />

这里有 options_menu.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />
</menu>

这是清单

<activity android:name=".SearchActivity"
              android:label="SearchActivity">

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>


    </activity>

唯一奇怪的是 options_menu.xml 文件中的这一行 (android:showAsAction="collapseActionView|ifRoom") 以红色突出显示。

尝试将其放入您的 options_menu.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/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_action_search"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="collapseActionView|ifRoom" />
</menu>

这在你的 AndroidManifest.xml

<activity android:name=".SearchActivity"
    android:label="SearchActivity">

    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>

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

    <meta-data android:name="android.app.default_searchable"
        android:value=".SearchActivity" />
</activity>

这在你的 SearchActivity.java

@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.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);

    return true;
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        Log.e("query", intent.getStringExtra(SearchManager.QUERY));
    }
}

供大家参考,希望对您有所帮助!

menu_main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
    android:title="@string/search_title"
    android:icon="@drawable/ic_search"
    android:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView" />

\res\xml\searchable.xml:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" />

AndroidManifest.xml:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

MainActivity.java:

package com.example.searchview;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().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.search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));


    return true;
}

...
}

试试下面的代码

menu_search.xml

     <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".SearchActivity">
    <item android:id="@+id/search"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />
</menu>

我认为你在上面的代码中犯了一个错误。 你在使用 appcompat 库吗? 如果没有,上面的代码应该可以工作。 如果是,请尝试使用 app:showAsAction 而不是 android:showAsActionapp:actionViewClass="android.support.v7.widget.SearchView" 而不是 android:actionViewClass="android.widget.SearchView"

清单

<activity
        android:name=".SearchActivity"
        android:label="SearchActivity">

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchActivity" />

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

    </activity>

SearchActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_search, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return true;
}

    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    System.out.println("onNewIntent SearchResults");
    handleIntent(intent);
}

private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        System.out.println("Received query: " + query);
    }
}