展开模式下的SearchView不会隐藏从棉花糖开始的所有操作栏图标 [英] SearchView in expanded mode doesn't hide all action bar icons starting from Marshmallow

查看:67
本文介绍了展开模式下的SearchView不会隐藏从棉花糖开始的所有操作栏图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SearchView在Lollipop设备(Android 21)上看起来不错:

SearchView looks fine on Lollipop devices (Android 21):

但是在Android 23-28上,它不会隐藏右侧的所有图标:

But on Android 23-28 it doesn't hide all icons on the right side:

<item android:id="@+id/action_search"
      android:title="@string/search"
      app:actionViewClass="androidx.appcompat.widget.SearchView"
      app:showAsAction="ifRoom"/>

<item android:id="@+id/action_sort"
      android:title="@string/sorting"
      android:icon="@drawable/sort"
      app:showAsAction="ifRoom"/>

我该如何解决?

更新

似乎Android 23及更高版本不再在右侧隐藏图标(因为有足够的空间)

Seems Android 23 and higher don't hide icons anymore on the right (because there is enough space)

但是只有在您没有左侧的主页按钮或汉堡菜单的情况下,它才能正常工作:

But it only works fine if you don't have home button or hamburger menu on the left:

但是我在左侧有一个图标,这就是为什么当SearchView展开时我的操作栏看起来难看的原因:

But I have an icon on the left and that's why my action bar looks ugly when SearchView is expanded:

应该是最新的Android ActionBar设计中的错误...

Should be a bug in newest Android ActionBar design...

我添加菜单按钮是这样的:

I add menu button like this:

supportActionBar?.let {
    it.setDisplayHomeAsUpEnabled(true)
    it.setHomeAsUpIndicator(R.drawable.ic_menu)
}

推荐答案

我的解决方案是在SearchView展开时以编程方式隐藏左侧的按钮(主菜单/菜单):

My solution is to hide the button on the left (home/menu) programmatically when SearchView is expanded:

val searchItem = menu.findItem(R.id.action_search)

val searchView = searchItem.actionView as SearchView

searchView?.setOnSearchClickListener {
    supportActionBar?.setDisplayHomeAsUpEnabled(false)
}
searchView?.setOnCloseListener {
    supportActionBar?.setDisplayHomeAsUpEnabled(true)
    false
}

p.s.下一个方法对我不起作用,因为它仅在您的SearchView始终处于扩展状态(标记为collapseActionView)时才起作用,但是我不希望我的应用程序中出现这种奇怪的事情(它不是搜索应用程序,搜索功能只是一个附加功能):

p.s. the next method didn't work for me because it works only if your SearchView is always expanded (marked as collapseActionView) but I don't want such weird thing in my app (it's not a search app, search function is just an additional feature):

searchItem.setOnActionExpandListener(object: MenuItem.OnActionExpandListener {
    override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
        supportActionBar?.setDisplayHomeAsUpEnabled(false)
        return true
    }

    override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        return true
    }
})

关闭:

展开:

这篇关于展开模式下的SearchView不会隐藏从棉花糖开始的所有操作栏图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆