Android Studio搜索小组件未显示在屏幕上

时间:2015-05-04 22:15:36

标签: java android xml

我一直在努力实现一个简单的搜索小部件而且它不起作用。我一直在寻找答案,并且发现过很多帖子,但没有答案对我有所帮助。该应用程序将运行,但不会显示搜索栏。以下是我遇到的一些资源:

这是我的代码:

MainActivity.java

package example.search.coco.mysearchapp;

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


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.options_menu, menu);

        // Associate searchable configuration with the SearchView
        //String pkg = "example.search.coco.mysearchapp";
        //String cls = "example.search.coco.mysearchapp.search.SearchActivity";
        //ComponentName mycomponent = new ComponentName(pkg,cls);
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        return true;
    }
}

SearchActivity.java

package example.search.coco.mysearchapp;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;

public class SearchResultsActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //use the query to search your data somehow
        }
    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

options_menu.xml

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

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="@string/search_hint" />

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.search.coco.mysearchapp" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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.default_searchable"
            android:value="example.search.coco.mysearchapp.SearchResultsActivity" />
        </activity>

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

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

    </application>

</manifest>

Source Code

过去几天我一直在尝试不同的事情,但似乎无法让它发挥作用。请帮忙!

1 个答案:

答案 0 :(得分:0)

在layout.xml中,AppTheme配置了NoActionBar。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

更改为

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

它有效。

相关问题