gpt4 book ai didi

java - Android searchview 删除提示填充

转载 作者:行者123 更新时间:2023-12-01 18:02:53 25 4
gpt4 key购买 nike

我正在我的应用程序中工作,工具栏中有一个搜索 View ,但提示在光标后面有一个小填充。

如何删除填充?

这是我的搜索 View :

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"/>

最佳答案

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

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

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

关于java - Android searchview 删除提示填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39333626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com