我正在做一个应用程序,例如当我点击图像时(它是一个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..."); } } }
Armin.. 48
在我的应用程序中我使用android.support.v7.widget.SearchView
并隐藏搜索图标这对我有用:
以编程方式也是可能的:searchView.setIconifiedByDefault(false); (2认同)
chjarder.. 28
我也遇到过麻烦.我结合了我找到的教程和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);
有关您需要更改的其他视图,请参阅本教程.
试试这三行:
android:iconifiedByDefault="false" android:searchIcon="@null" android:searchHintIcon="@null"
*它仅适用于android.widget.SearchView
我只是把@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>
适合我.
TL; DR:只需将EditText
提示设置为空字符串就足以删除提示图标.
我找到了一个真正有效的答案.使用反射更改图标有很多内容 - 例如在Styling 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(); }
您应该将它放在您的onCreateOptionsMenu
方法中,并使用以下内容获取对XML
-declared 的引用SearchView
:
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
我也遇到过麻烦.我结合了我找到的教程和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);
有关您需要更改的其他视图,请参阅本教程.
在我的应用程序中我使用android.support.v7.widget.SearchView
并隐藏搜索图标这对我有用:
<android.support.v7.widget.SearchView ... app:iconifiedByDefault="false" app:searchIcon="@null" />