【发布时间】:2017-07-06 08:45:01
【问题描述】:
问题
我使用了官方 Android 文档中的 Setting Up The Search interface 文档,但由于某些奇怪的原因,我并没有走得太远,因为 SearchView 拒绝在 Toolbar 中打开
cfr. Android docs
代码
我已经添加了这样的菜单项:
<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="...">
<item
android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"/>
</menu>
(旁注,我也尝试过 v7 SearchView 视图类,但结果没有什么不同)
这就是工具栏的布局方式
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
app:theme="@style/AppTheme.Toolbar"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
我还在清单中添加了SEARCH 意图,并且菜单在onCreateOptionsMenu 方法中得到正确膨胀。
编辑、manifest.xml 和 searchable.xml 根据要求添加了这些,尽管我认为它们与问题无关。
<activity
android:name=".HomeActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
可搜索的.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="" />
现在根据 Android 文档,SearchView 应该会出现,但是当我按下 Toolbar 中的搜索图标时,什么也没有发生。
有什么想法可能是错的吗?
【问题讨论】:
-
添加您的清单文件和带有问题的 searchable.xml 文件
-
@NileshRathod 我已经添加了清单和可搜索的 xml
标签: android android-toolbar searchview android-search