繁体   English   中英

Android SearchView删除提示填充

[英]Android searchview remove hint padding

我正在使用我的应用程序,并且在工具栏内有一个searchview,但是提示在光标后有一个小填充。

如何删除填充物?

这是我的搜索视图:

搜索视图

我想要:

没有填充的searchview

这是我的onCreateOptionsMenu:

SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    MenuItem item=menu.findItem(R.id.procura);
    Drawable drawable = menu.findItem(R.id.procura).getIcon();
    if (drawable != null) {
        drawable.mutate();
        drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
    }

    searchView=(SearchView) MenuItemCompat.getActionView(item);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setQueryHint(getResources().getString(R.string.app_hint));

    try {
        Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
        mDrawable.setAccessible(true);
        Drawable drw = (Drawable) mDrawable.get(searchView);
        drw.setBounds(0, 0, 0, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

我的风格:

<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
    <item name="submitBackground">@color/colorPrimary</item>
    <item name="queryBackground">@color/colorPrimary</item>
    <item name="android:colorFocusedHighlight">@color/colorAccent</item>
</style>

我的菜单:

<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=".MainActivity">
<item
    android:id="@+id/procura"
    android:title="@string/app_hint"
    android:icon="@mipmap/ic_search_black_24dp"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

我找到了解决我问题的方法。

只需添加以下样式即可:

<item name="searchHintIcon">@null</item>

现在的样式是:

<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
    <item name="submitBackground">@color/colorPrimary</item>
    <item name="queryBackground">@color/colorPrimary</item>
    <item name="searchHintIcon">@null</item>
    <item name="android:colorFocusedHighlight">@color/colorAccent</item>
</style>

现在onCreateOptionsMenu是:

// Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);


    SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    MenuItem item=menu.findItem(R.id.procura);

    searchView=(SearchView) MenuItemCompat.getActionView(item);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setQueryHint(getResources().getString(R.string.app_hint));


    return true;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM