【问题标题】:Searchview not working inside toolbar androidSearchview在工具栏android内部不起作用
【发布时间】:2016-11-08 04:38:46
【问题描述】:

当我单击工具栏内的搜索图标时,搜索栏正在响应(不可见) 菜单.xml

<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="search_title"
    android:icon="@drawable/search"
    app:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView" />

可搜索的.xml

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

清单文件

 <activity
        android:name=".SearchbarActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">

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

        <!-- This metadata entry provides further configuration details for searches -->
        <!-- that are handled by this activity. -->

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

搜索栏活动

  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_search, menu);
    SearchManager searchManager = (SearchManager)
            getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchMenuItem = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) searchMenuItem.getActionView();



    return true;
}

v21/styles.xml

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowActivityTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">false</item>
    <item name="android:windowAllowReturnTransitionOverlap">false</item>
</style>

styles.xml

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="Splash" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splashscreen</item>

</style>

我已经扩展了 AppCompatActivity。我之前尝试过相同的代码并且我得到了它的工作,之后我在 styles.xml 和 v21/styles.xml 中进行了更改,我认为这造成了问题。谢谢你。

【问题讨论】:

    标签: android android-actionbar material-design android-toolbar searchbar


    【解决方案1】:

    使用以下代码 并实现SearchView.OnQueryTextListener

    根据您的要求进行其他操作(如果您得到解决方案,请将其标记为已接受的答案,甚至您可以寻求更多相关帮助)

        SearchView searchView =  (SearchView)menu.findItem(R.id.search).getActionView();
        int searchImgId = android.support.v7.appcompat.R.id.search_button;
        ImageView v = (ImageView) searchView.findViewById(searchImgId);
        searchView.setOnQueryTextListener(this);
    

    【讨论】:

    • 嘿,谢谢回复。我试过上面的代码,它不起作用。检查styles.xml和v21/styles.xml。我认为工具栏或主题有问题。
    • 你为什么要使用 从 manifest.xml 中删除后尝试代码
    • 告诉我错误消息或日志消息,以便我可以帮助您解决问题?当你点击菜单的搜索图标时会发生什么?
    • 实际上应用程序没有崩溃也没有错误,工具栏里面的搜索栏没有出现
    【解决方案2】:

    collapseActionView 从显示中删除为操作。 只需添加app:showAsAction="ifRoom"

    【讨论】:

    • 嘿,谢谢你的回复。我试过ifroom,总是不行。检查styles.xml和v21/styles.xml。我认为工具栏或主题有问题。
    猜你喜欢
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 2015-02-07
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多