Android 删除 SearchIcon 作为搜索视图中的提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20323990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove the SearchIcon as hint in the searchView
提问by Hummer
I am doing an application where for example when i click on the image(it is a searchView)
我正在做一个应用程序,例如当我点击图像时(它是一个搜索视图)
the search pad opens !
搜索板打开!
and it looks like
它看起来像
but here the default search icon (magnifier) gets displayed but this dissappears as soon as some text is entered
但是这里显示了默认的搜索图标(放大镜),但是一旦输入了一些文本,它就会消失
but i dont want that magnifier to be displayed even the image is clicked for the first time
但即使是第一次单击图像,我也不希望显示该放大镜
and here i am not using any xml file
在这里我没有使用任何 xml 文件
my code is
我的代码是
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout relative = new RelativeLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
relative.setLayoutParams(params);
setContentView(relative);
SearchView searchView = new SearchView(this);
traverseView(searchView, 0);
// searchView.setIconifiedByDefault(false);
LayoutParams searchViewparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
// searchViewparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
searchView.setLayoutParams(searchViewparams);
relative.addView(searchView);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
private void traverseView(View view, int index) {
if (view instanceof SearchView) {
SearchView v = (SearchView) view;
for(int i = 0; i < v.getChildCount(); i++) {
traverseView(v.getChildAt(i), i);
}
} else if (view instanceof LinearLayout) {
LinearLayout ll = (LinearLayout) view;
for(int i = 0; i < ll.getChildCount(); i++) {
traverseView(ll.getChildAt(i), i);
}
} else if (view instanceof EditText) {
((EditText) view).setTextColor(Color.GREEN);
((EditText) view).setHintTextColor(Color.BLACK);
} else if (view instanceof TextView) {
((TextView) view).setTextColor(Color.BLUE);
} else if (view instanceof ImageView) {
((ImageView) view).setImageResource(R.drawable.ic_launcher);
} else {
Log.v("View Scout", "Undefined view type here...");
}
}
}
采纳答案by chjarder
I've had trouble with this too. I combined the tutorial I found and an existing answer found here in stackoverflow.
我也遇到过这个问题。我结合了我找到的教程和在 stackoverflow 中找到的现有答案。
int magId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView magImage = (ImageView) searchView.findViewById(magId);
magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
Take note that searchView is a LinearLayout, so use LinearLayout.LayoutParams to avoid an exception.
注意searchView 是一个LinearLayout,所以使用LinearLayout.LayoutParams 来避免异常。
I also tried this but it doesn't remove the view. I can't seem to figure why.:
我也试过这个,但它不会删除视图。我似乎无法弄清楚为什么。:
magImage.setVisibility(View.GONE);
For the other views that you need to change, refer to this tutorial.
对于其他需要更改的视图,请参阅本教程。
回答by Armin
In my app i've used android.support.v7.widget.SearchView
and to hide search icon this worked for me :
在我的应用程序中,我使用android.support.v7.widget.SearchView
并隐藏了这对我有用的搜索图标:
<android.support.v7.widget.SearchView
...
app:iconifiedByDefault="false"
app:searchIcon="@null"
/>
回答by Tomask
I've simply put @nullto searchHintIcon
attribute:
我只是把@null到searchHintIcon
属性:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="searchHintIcon">@null</item>
</style>
and applied that style in my app theme
并在我的应用程序主题中应用该样式
<style name="Theme.App" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
<item name="searchViewStyle">@style/SearchViewMy</item>
</style>
Works for me.
为我工作。
回答by Silvio Guedes
Try these three lines:
试试这三行:
android:iconifiedByDefault="false"
android:searchIcon="@null"
android:searchHintIcon="@null"
*It works only for android.widget.SearchView
*它仅适用于 android.widget.SearchView
回答by Phil
TL;DR: Simply setting the EditText
's hint to the empty string is enough to remove the hint icon.
TL;DR:只需将EditText
的提示设置为空字符串就足以删除提示图标。
I found an answer that actually works. There's a lot of stuff on changing the icon using reflection - such as discussed at Styling the ActionBar SearchView. This website ISa great resource, but unfortunately the search hint ImageView
does not change (even though the reflection does not cause errors). The only way that I was able to remove this image was this:
我找到了一个真正有效的答案。有很多关于使用反射更改图标的内容 - 例如在样式化 ActionBar SearchView 中讨论的内容。这个网站是一个很好的资源,但不幸的是搜索提示ImageView
没有改变(即使反射不会导致错误)。我能够删除此图像的唯一方法是:
try {
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setHint("");
}
catch (Throwable t)
{
t.printStackTrace();
}
You should place this in your onCreateOptionsMenu
method, and get a reference to your XML
-declared SearchView
using something like:
您应该将它放在您的onCreateOptionsMenu
方法中,并使用以下内容获取对您的XML
-declared的引用SearchView
:
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
回答by Chao Zhang
I just met the same problem, here are some reference links.
我刚遇到同样的问题,这里有一些参考链接。
http://blog.luxteam.net/2013/11/04/styling-appcompat-searchview/
http://blog.luxteam.net/2013/11/04/styling-appcompat-searchview/
The key is in the following function, it sets the icon as an ImageSpan
.
关键在以下函数中,它将图标设置为ImageSpan
.
private CharSequence getDecoratedHint(CharSequence hintText) {
// If the field is always expanded, then don't add the search icon to the hint
if (!mIconifiedByDefault) return hintText;
SpannableStringBuilder ssb = new SpannableStringBuilder(" "); // for the icon
ssb.append(hintText);
Drawable searchIcon = getContext().getResources().getDrawable(getSearchIconId());
int textSize = (int) (mQueryTextView.getTextSize() * 1.25);
searchIcon.setBounds(0, 0, textSize, textSize);
ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return ssb;
}
回答by Rahul
Simple solution is searchHintIconattribute
简单的解决方案是searchHintIcon属性
<style name="SearchViewStyleMyApp" parent="Widget.AppCompat.SearchView">
<!-- Background for the search query section (e.g. EditText) -->
<item name="queryBackground">@android:color/white</item>
<!-- note that this is how you style your hint icon -->
<item name="searchHintIcon">@null</item>
<!-- The hint text that appears when the user has not typed anything -->
<item name="queryHint">@string/search_hint</item>
</style>
回答by Vladimir Farafonov
Use "searchHintIcon" attribute in your styles for that:
为此在您的样式中使用“searchHintIcon”属性:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="searchViewStyle">@style/AppTheme.SearchView</item>
</style>
<style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
<item name="searchHintIcon">@null</item>
</style>
You can replace @null with any drawable res you want
你可以用任何你想要的可绘制资源替换@null
回答by roghayeh hosseini
It's answer
这是答案
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);
回答by Pranay Bhaturkar
For androidx you can do like this
对于 androidx 你可以这样做
ImageView magImage = (ImageView) searchView.findViewById(androidx.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);
magImage.setImageDrawable(null);
It's work for me
这对我有用