在searchView中删除SearchIcon作为提示

问题描述 投票:27回答:11

我正在做一个应用程序,例如当我点击图像时(它是一个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...");
        }
    }
}
android android-image searchview
11个回答
27
投票

我也遇到过麻烦。我结合了我找到的教程和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.


0
投票

使用v7 Widget,如果使用searchView.IconifiedByDefault(false),搜索视图的行为会有所不同。您只在xml中将提示图标设置为@null。您也可以将此方式应用于其他图标。


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>

42
投票

在我的应用程序中,我使用了android.support.v7.widget.SearchView并隐藏搜索图标,这对我有用:

<android.support.v7.widget.SearchView
    ...
    app:iconifiedByDefault="false"
    app:searchIcon="@null"
/>

22
投票

我只是把@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>

适合我。


17
投票

试试这三行:

android:iconifiedByDefault="false"
android:searchIcon="@null"
android:searchHintIcon="@null"

*它仅适用于android.widget.SearchView


5
投票

TL; DR:只需将EditText的提示设置为空字符串就足以删除提示图标。

我找到了一个真正有效的答案。使用反射更改图标有很多东西 - 比如在Styling the 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声明的SearchView

SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

4
投票

我刚遇到同样的问题,这里有一些参考链接。

http://blog.luxteam.net/2013/11/04/styling-appcompat-searchview/

https://android.googlesource.com/platform/frameworks/support.git/+/android-support-lib-19.1.0/v7/appcompat/src/android/support/v7/widget/SearchView.java

关键是在以下功能中,它将图标设置为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;
}

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


1
投票

这是答案

ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);

ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);

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...");
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.