[机器人搜索小插件]如何隐藏的默认搜索视图的关闭按钮? [英] [Android search widget]How to hide the close button in search view by default?

查看:184
本文介绍了[机器人搜索小插件]如何隐藏的默认搜索视图的关闭按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现在操作栏中的机器人搜索视图。当搜索视图获得焦点的关闭按钮[X],在右侧显示出来。我看了看其他Android原生应用,如联系人和Gmail。当搜索视图获得焦点的关闭按钮不会显示。 如何设置我的搜索行为来看这样?

I have implemented an android search view in action bar. When the search view gains focus, the close button [x] at the right shows up. I took a look at other android native apps, like Contacts and Gmail. The close button is not shown when the search view gains focus. How to set my search view behaves like this?

推荐答案

我面临同样的问题与 android.support.v7.widget.SearchView ,并找到了解决办法。首先,在 onCreateOptionsMenu ,你可以得到一个参考搜索查看以及它的关闭按钮:

I faced the same problem with android.support.v7.widget.SearchView and found a solution. First, in onCreateOptionsMenu, you can obtain a reference to the SearchView as well as its close button:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.search, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    try {
        Field searchField = SearchView.class.getDeclaredField("mCloseButton");
        searchField.setAccessible(true);
        mSearchCloseButton = (ImageView) searchField.get(mSearchView);
    } catch (Exception e) {
        Log.e(TAG, "Error finding close button", e);
    }
}

现在,你可以尝试修改按钮。首先,我试图用 setVisibility(View.GONE)隐藏的关闭按钮,但是,这并不工作,因为搜索查看重置其关闭按钮时,用户的可视性交互与搜索查看。所以我的解决方案是使用一个透明的绘制和禁用关闭按钮的点击:

Now you can try to modify the button. First I tried to use setVisibility(View.GONE) to hide the close button, but that doesn't work, because the SearchView resets the visibility of its close button when the user interacts with the SearchView. So my solution was to use a transparent drawable and disable the click of the close button:

if (mSearchCloseButton != null) {
    mSearchCloseButton.setEnabled(false);
    mSearchCloseButton.setImageDrawable(getResources().getDrawable(R.drawable.transparent));
}

这篇文章帮助我,以及:
<一href="http://novoda.com/blog/styling-actionbar-searchview">http://novoda.com/blog/styling-actionbar-searchview

This article helped me as well:
http://novoda.com/blog/styling-actionbar-searchview

不过,这是相当哈克说实话。这将是清洁,以抓住从搜索查看源<一href="https://android.googlesource.com/platform/frameworks/support.git/+/master/v7/appcompat/src/android/support/v7/widget/SearchView.java">https://android.googlesource.com/platform/frameworks/support.git/+/master/v7/appcompat/src/android/support/v7/widget/SearchView.java并创建自己的版本,搜索查看,做隐藏/显示关闭按钮的。

However, this is quite hacky to be honest. It would be cleaner to grab the SearchView source from https://android.googlesource.com/platform/frameworks/support.git/+/master/v7/appcompat/src/android/support/v7/widget/SearchView.java and create your own version of SearchView that does the hide/show of the close button.

更新:
谷歌刚刚宣布AppCompat V21,它具有造型改进了搜索查看窗口小部件:
<一href="http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-$p$p.html">http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-$p$p.html

Update:
Google just announced AppCompat v21, which has styling improvements for the SearchView widget:
http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

这篇关于[机器人搜索小插件]如何隐藏的默认搜索视图的关闭按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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