更改SearchView小放大镜图标

时间:2014-08-20 00:57:23

标签: android searchview

我想在EditText上更改看起来像drawableLeft的{​​{1}}的小放大镜图标。

我尝试了以下内容:

This link显示整个搜索窗口小部件的布局xml 文件。所以使用这种方法(类似于反射)我改变了每个图标除了小放大镜

SearchView

我尝试更改int submitButtonId = searchView.getContext().getResources().getIdentifier("android:id/search_go_btn", null, null); ImageView imageView = (ImageView) searchView.findViewById(submitButtonId); imageView.setImageResource(submitIcon); android:id/search_mag_icon,但android:id/search_button内的小放大镜仍为灰色(我只想要一个白色)。

仍未击败,我继续做出一些假设:

由于它不起作用,它必须同时为EditText(对应ImageViewsandroid:id/search_mag_icon)然后必须表示图标是要么 drawablePadding 后台,要么以编程方式设置。 “布局文件中没有android:id/search_button也没有android:drawableLeft,因此第一个选项错误...

因此,我继续确认它是否确实是background NOPE ,背景只覆盖蓝线。

所以,如果小放大镜android:drawableRight 也不 a ImageView a {{1 },It must have been set programmatically(查找嵌套类drawableLeft/Right但不是!!!

那么,我该如何更改这个小放大镜图标呢?我真的尝试了一切

2 个答案:

答案 0 :(得分:3)

这个问题花了我大约半个小时的时间来写,因为我想排除我已经做过的事情,但是在发布之后我找到了答案5分钟:http://nlopez.io/how-to-style-the-actionbar-searchview-programmatically/

PS:是的,一路反射

答案 1 :(得分:0)

如果有人遇到这个问题,对我有用的是:

    searchView.setIconifiedByDefault(false); //This is what does the trick. Removing the magnifier icon.

    ImageView searchHintIcon = (ImageView) searchView.findViewById(R.id.search_mag_icon);
    searchHintIcon.setImageResource(R.drawable.ic_search_black);

以下是整个方法:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.home_search, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.search_action).getActionView();

    //If, this one returns null, use second option.
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getActivity().getComponentName());
    if (searchableInfo == null) {
        searchableInfo = searchManager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchActivity.class));
    }
    searchView.setSearchableInfo(searchableInfo);
    searchView.setIconifiedByDefault(false);

    try {
        EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        searchEditText.setTextColor(getResources().getColor(R.color.black));
        searchEditText.setHintTextColor(getResources().getColor(R.color.black));
        ImageView searchHintIcon = (ImageView) searchView.findViewById(R.id.search_mag_icon);
        searchHintIcon.setImageResource(R.drawable.ic_search_black);

        Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
        f.setAccessible(true);
        f.set(searchEditText, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
相关问题