[英]Removing default search icon from SearchView widget
How can i remove default search icon which appears as a hint in SearchView widget(not in actionbar)?Through styles i can change the icon but i can't find a way to remove it?如何删除在 SearchView 小部件中显示为提示的默认搜索图标(不在操作栏中)?通过样式我可以更改图标,但我找不到删除它的方法?
It's Worked......成功了……
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);
For me it only worked this:对我来说,它只适用于这个:
ImageView magImage = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);
magImage.setImageDrawable(null);
@sector11's almost got it, but still you need to add the line magImage.setImageDrawable(null)
because taking a look at SearchView.java
in Github,specifically the method updateViewsVisibility , you can see that it constantly gets its visibility updated @sector11差不多明白了,但你仍然需要添加
magImage.setImageDrawable(null)
因为SearchView.java
Github 中的SearchView.java
,特别是方法updateViewsVisibility ,你可以看到它不断更新其可见性
Extract from GitHub摘自 GitHub
if (mCollapsedIcon.getDrawable() == null || mIconifiedByDefault) {
iconVisibility = GONE;
} else {
iconVisibility = VISIBLE;
}
mCollapsedIcon.setVisibility(iconVisibility);
As of now you should use截至目前,您应该使用
ImageView magImage = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);
Try this one:试试这个:
<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView" >
<item name="searchHintIcon">@null</item>
<!-- Background for the search query section (e.g. EditText) -->
<item name="queryBackground">@color/transparent</item>
</style>
Never use direct android ids to find the view because in future if the ids change, your code will not work.永远不要使用直接的 android id 来查找视图,因为将来如果 id 更改,您的代码将无法工作。 Always use standard ways to solve the problems instead of a workaround.
始终使用标准方法而不是变通方法来解决问题。 for more info refer this link .
有关更多信息,请参阅此链接。
对于 androidx SearchView 只需使用app:searchIcon="@null"
you can override it:你可以覆盖它:
int searchImgId = context.getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView searchImage = (ImageView) searchView.findViewById(searchImgId);
searchImage.setVisibility(View.GONE);
If your search view is in layout then please use EditText with close button:如果您的搜索视图在布局中,请使用带有关闭按钮的 EditText:
https://github.com/yanchenko/droidparts/blob/develop/droidparts/src/org/droidparts/widget/ClearableEditText.java https://github.com/yanchenko/droidparts/blob/develop/droidparts/src/org/droidparts/widget/ClearableEditText.java
If you want to create your own then use frame layout and add text change listener to mange visibility of close or cancel button.如果您想创建自己的布局,请使用框架布局并添加文本更改侦听器来管理关闭或取消按钮的可见性。
there are few discussion regarding search view in UI:关于 UI 中搜索视图的讨论很少:
How to create EditText with cross(x) button at end of it? 如何在其末尾创建带有十字(x)按钮的 EditText?
How to place button inside of edit text 如何将按钮放在编辑文本内
If you want to show search list then use AutoCompleteTextView instead of EditText.如果要显示搜索列表,请使用 AutoCompleteTextView 而不是 EditText。
you can read tutorial from below link:您可以从以下链接阅读教程:
2. http://examples.javacodegeeks.com/android/core/widget/autocompletetextview/android-auto-complete-example/ 2. http://examples.javacodegeeks.com/android/core/widget/autocompletetextview/android-auto-complete-example/
3. http://androidexample.com/Show_AutoComplete_Suggestions_-_Android_Example/index.php?view=article_discription&aid=105&aaid=127 3. http://androidexample.com/Show_AutoComplete_Suggestions_-_Android_Example/index.php?view=article_discription&aid=105&aaid=127
I hope it will work.我希望它会起作用。
please set this SearchView in your code请在您的代码中设置此 SearchView
android:paddingLeft="-16dp" // this line is remove the space of icon
android:paddingStart="-16dp" // // this line is remove the space of icon
android:searchIcon="@null" // this line to remove the search icon
android:closeIcon="@null" // this line to remove the close icon
<SearchView
android:queryBackground="@android:color/transparent"
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="-16dp"
android:paddingStart="-16dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@null"
android:searchIcon="@null"
android:closeIcon="@null"
android:theme="@style/BaseTheme"
android:iconifiedByDefault="false"
android:queryHint="@string/hint">
<requestFocus />
</SearchView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.