我正在做一个应用程序,例如当我点击图像时(它是一个searchView)
搜索板打开!
看起来像
但此处会显示默认搜索图标(放大镜),但只要输入了一些文字就会消失
但我不希望显示放大镜,即使是第一次点击图像
这里我没有使用任何xml文件
我的代码是
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...");
}
}
}
答案 0 :(得分:38)
在我的应用中,我使用了android.support.v7.widget.SearchView
并隐藏了搜索图标,这对我有用:
<android.support.v7.widget.SearchView
...
app:iconifiedByDefault="false"
app:searchIcon="@null"
/>
答案 1 :(得分:25)
我也遇到过这个问题。我结合了我找到的教程和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));
请注意,searchView是一个LinearLayout,因此请使用LinearLayout.LayoutParams来避免异常。
我也试过了,但它没有删除视图。我似乎无法理解为什么。:
magImage.setVisibility(View.GONE);
对于您需要更改的其他视图,请参阅to this tutorial.
答案 2 :(得分:21)
我只是将 @null 添加到searchHintIcon
属性:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="searchHintIcon">@null</item>
</style>
并在我的应用主题中应用该风格
<style name="Theme.App" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
<item name="searchViewStyle">@style/SearchViewMy</item>
</style>
适合我。
答案 3 :(得分:14)
尝试这三行:
android:iconifiedByDefault="false"
android:searchIcon="@null"
android:searchHintIcon="@null"
*仅适用于android.widget.SearchView
答案 4 :(得分:5)
TL; DR:只需将EditText
的提示设置为空字符串即可删除提示图标。
我找到了一个真正有效的答案。使用反射更改图标有很多内容 - 例如在Styling the ActionBar SearchView中讨论过的。这个网站 IS 是一个很好的资源,但不幸的是搜索提示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();
}
您应该将此放在onCreateOptionsMenu
方法中,并使用以下内容获取对XML
- 声明SearchView
的引用:
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
答案 5 :(得分:4)
我刚遇到同样的问题,这里有一些参考链接。
http://blog.luxteam.net/2013/11/04/styling-appcompat-searchview/
键位于以下功能中,它将图标设置为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;
}
答案 6 :(得分:2)
在样式中使用“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>
您可以将@null替换为您想要的任何可绘制的res
答案 7 :(得分:1)
是答案
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);
答案 8 :(得分:1)
对于androidx,您可以这样做
ImageView magImage = (ImageView) searchView.findViewById(androidx.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);
magImage.setImageDrawable(null);
这对我有用
答案 9 :(得分:0)
你可以试试这个:
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);
searchView.setIconifiedByDefault(false);
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...");
}
}
}
答案 10 :(得分:0)
对于v7窗口小部件,如果您使用searchView.setIconifiedByDefault(false),则搜索视图的行为会有所不同。您只能在xml中将提示图标设置为@null。您也可以将这种方式应用于其他图标。
答案 11 :(得分:0)
简单的解决方案是 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>
答案 12 :(得分:0)
如果您使用androidx.appcompat.widget.SearchView:
您可以使用以下方法隐藏图标:
val magImage = searchView.findViewById<View>(androidx.appcompat.R.id.search_mag_icon) as ImageView
magImage.layoutParams = LinearLayout.LayoutParams(0, 0)