ActionBar always expanded SearchView with the icon inside

早过忘川 提交于 2019-12-01 07:24:34

问题


I have an ActionBar with a SearchView.

I want the search view to always be "open" (expanded). Doing that is possible using

mSearchView.setIconifiedByDefault(false);

But it for some reason moves the search icon outside of the text area:

If I don't use setIconifiedByDefault(), it looks like I want it to look when it is expanded:

But then it collapses into a search icon on back, and also

MenuItemCompat.expandActionView(searchItem);

Does not seem to help.

Is there a way to achieve what I want?


回答1:


You can just intercept OnClose event:

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
    @Override
    public boolean onClose() {
        return true;
    }
});



回答2:


I was able to solve this issue by changing my XML from

<item android:id="@+id/search"
android:icon="@drawable/my_icon"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />

to

<item android:id="@+id/search"
android:icon="@drawable/my_icon"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView" />


来源:https://stackoverflow.com/questions/24757591/actionbar-always-expanded-searchview-with-the-icon-inside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!