Android searchview删除提示填充

时间:2016-09-05 15:21:43

标签: java android searchview

我在我的应用中工作并在工具栏中有一个搜索视图,但提示在光标后面有一个小填充。

如何删除填充?

这是我的搜索视图:

searchview

我希望如此:

searchview with no padding

这是我的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"/>

1 个答案:

答案 0 :(得分:3)

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

只需在样式中添加:

<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;