可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How to remove or change the search view icon inside the edittext?
I am using the Appcompat library.
I used the below code to modify and remove but it's not working:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
View search_mag_icon = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
search_mag_icon.setBackgroundResource(R.drawable.ic_stub);
//search_mag_icon.setVisibility(View.GONE);
回答1:
Finally found the solution. Try the following:
try {
Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
mDrawable.setAccessible(true);
Drawable drawable = (Drawable)mDrawable.get(your search view here);
drawable.setAlpha(0);
} catch (Exception e) {
e.printStackTrace();
}
回答2:
Per the AppCompat v21 blog post, you can provide a custom style for your SearchView
, overriding things such as the search icon:
<style name="Theme.MyTheme" parent="Theme.AppCompat">
<item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>
<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
<!-- Search button icon -->
<item name="searchIcon">@drawable/custom_search_icon</item>
</style>
回答3:
You might use below lines of code:
int searchMagIcon = getResources().getIdentifier("search_mag_icon", "id", "android");
ImageView SearchHintIcon = (ImageView) searchView.findViewById(searchMagIcon);
That will save the ImageView and then you can change it.
回答4:
I used the menu item with the property app:actionViewClass="android.support.v7.widget.SearchView"
On the activity I set:
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setIconifiedByDefault(false);
To remove the icon I did:
ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
searchIcon.setImageDrawable(null);
回答5:
no a good idea use private method as SearchView.class.getDeclaredField("mSearchHintIcon");
<item android:id="@+id/menu_search"
android:icon="@drawable/action_search"
app:actionLayout="@layout/xx_layout"
app:showAsAction="always"/>
and in xx_layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:searchHintIcon="@null"
android:iconifiedByDefault="true" />
回答6:
remove is the right drawable that you have mentioned in xml:
final Drawable remove = ContextCompat.getDrawable(getActivity(), R.drawable.ic_close_circle);
etSearchProducts.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (etSearchProducts.getCompoundDrawables()[2] == null) {
return false;
}
if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
if (event.getX() > etSearchProducts.getWidth() - etSearchProducts.getPaddingRight() - remove.getIntrinsicWidth()) {
etSearchProducts.setText("");
Utility.hideKeyboard(getActivity());
}
return false;
}
});
回答7:
Simplest way is
In case you are using AppCompat
use
app:iconifiedByDefault="false"
app:searchIcon="@null"
Else use android
in place of app
in above code as
android:iconifiedByDefault="false"
android:searchIcon="@null"