android - 为什么 android.widget.SearchView 在单击时不展开,而是将图标移动到左侧?
问题描述
我在android.widget.SearchView
里面使用MaterialToolbar
并且还在菜单资源文件中设置了正确的属性。但是当我点击搜索图标时,它并没有展开。相反,搜索图标移动到左侧。再次单击它时,它会展开,但搜索图标仍会出现在 EditText 中。
布局文件
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:title="@string/app_name" />
</com.google.android.material.appbar.AppBarLayout>
菜单文件
<?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/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.widget.SearchView" />
</menu>
点击之前
点击后
解决方案
尽管文档建议使用 framework SearchView
,但我一直发现 support/androidxSearchView
与库组件(例如 、AppCompatActivity
等)配合得更好,MaterialToolbar
尽管我不确定导致这些小故障的确切原因。确实,使用androidx.appcompat.widget.SearchView
此处代替了android.widget.SearchView
在actionViewClass
扩展时摆脱了那个放错位置的搜索图标。
但是,AutoCompleteTextView
内部SearchView
仍然有一个类似的搜索图标作为提示,因为它没有以正确的样式结束。我最初预计将 设置Toolbar
为支持ActionBar
会将其与儿童的其他相关样式整合在一起,但似乎'的SearchView
样式由于某种原因通常设置为.ThemeOverlay.*.ActionBar
<*Toolbar>
ActionBar
尽管大多数消息来源似乎表明各种ThemeOverlay.*.ActionBar
样式仅调整colorControlNormal
属性,但它们实际上也将 设置searchViewStyle
为适当的Widget.*.SearchView.ActionBar
值,因此添加适当的叠加层非常重要。例如,与更改为androidx
版本保持一致:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
... />
这也可以通过将其设置为actionBarTheme
您Activity
的主题来实现,但请注意,它可以被<*Toolbar>
自身的属性覆盖,就像在给定的设置中一样style="@style/Widget.MaterialComponents.Toolbar.Primary"
。
如果您不使用材质组件,ThemeOverlay.AppCompat
也可以使用样式。如果你只使用平台类,在系统命名空间中可以使用类似的样式;例如,@android:style/ThemeOverlay.Material.Dark.ActionBar
。
该答案的初始修订版手动删除了该提示图标,因为当时我不知道给定设置究竟是如何失败的。现在不需要这样做,但是如果您想进一步自定义它,该示例只需将 menu<item>
的app:actionViewClass
属性替换为app:actionLayout
指向此布局的属性:
<androidx.appcompat.widget.SearchView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:searchHintIcon="@null" />
该searchHintIcon
设置是此处示例所需的全部内容,但您可以设置任何SearchView
您想要的适用属性。
如果你要走这条路,最好设置style="@style/Widget.AppCompat.SearchView.ActionBar"
,其中包括searchHintIcon
设置,并确保正确的整体风格SearchView
,正如 Artem Mostyaev 在下面的评论中所建议的那样。
推荐阅读
- sql-server-2008 - 从另一个表中插入数据 order by
- shell - Shell:非 ASCII 文本的 printf 对齐方式不一致
- android - DiffUtil 将错误的位置传递给 areContentsTheSame 方法
- python - 如何通过 cerberus 验证其字段可以是 dict 或 dict 列表的数据?
- angular - 如何从代码中将选择设置为 angular7-8 中的谷歌图表
- postgresql - 在 PostgreSQL 中为一个过程获取多个结果集
- php - 使用 laravel-passport 进行 Swagger API 身份验证
- c# - 如何按 ToLower() 分组?
- c# - 有没有办法访问一个类的成员,在访问时使用字符串而不是类名
- php - 从 bin2hex 函数压缩 PHP 中的十六进制数据