Android searchview 删除提示填充

Bru*_*ira 1 java android 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();
    }
Run Code Online (Sandbox Code Playgroud)

我的风格:

<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>
Run Code Online (Sandbox Code Playgroud)

我的菜单:

<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"/>
Run Code Online (Sandbox Code Playgroud)

Bru*_*ira 5

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

只需将其添加到样式中即可:

<item name="searchHintIcon">@null</item>
Run Code Online (Sandbox Code Playgroud)

现在的风格是:

<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>
Run Code Online (Sandbox Code Playgroud)

现在的 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;
Run Code Online (Sandbox Code Playgroud)