簡體   English   中英

搜索圖標操作按鈕未顯示在操作欄中 - 無法訪問搜索功能

[英]Search icon action button not displaying in action bar - no way to access search function

我使用以下內容作為指南為RecyclerView創建了一個搜索功能:

如何使用SearchView過濾RecyclerView

我無法測試它,因為所有顯示的都是帶有應用標題的應用欄。 我期待(希望!)看到一個搜索圖標操作按鈕,我點擊該按鈕打開一個搜索字段。 (類似於附件中的演示)。

我有以下代碼適用於啟動搜索菜單:

search_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="@android:drawable/ic_menu_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:showAsAction="always|ifroom"
/>
</menu>

這是我的片段中的代碼:

 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
 inflater.inflate(R.menu.search_menu, menu);

 final MenuItem item = menu.findItem(R.id.search);

 final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
 searchView.setOnQueryTextListener(this);

}

我已經瀏覽了android開發人員指南,但仍然無法弄明白。 任何人都可以建議我是否遺漏了什么? 或者知道它為什么不顯示? 我完全難過了!

不要覆蓋任何與菜單相關的方法,如onMenuCreatedonMenuItemSelected

在MainActivity XML中添加此代碼

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >

            <android.support.v7.widget.SearchView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/search"/>
        </android.support.v7.widget.Toolbar>


    </android.support.design.widget.AppBarLayout>

然后在您的片段中,訪問搜索視圖,就像使用ID訪問其他視圖一樣。

您正在嘗試將搜索圖標投射到搜索視圖,進行自定義布局,然后就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM