在searchView中删除SearchIcon作为提示

 Christy-1221 发布于 2023-02-13 14:41

我正在做一个应用程序,例如当我点击图像时(它是一个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);

有关您需要更改的其他视图,请参阅本教程.

5 个回答
  • 试试这三行:

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

    *它仅适用于android.widget.SearchView

    2023-02-13 14:43 回答
  • 我只是把@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>
    

    适合我.

    2023-02-13 14:43 回答
  • 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();
    

    2023-02-13 14:43 回答
  • 我也遇到过麻烦.我结合了我找到的教程和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);
    

    有关您需要更改的其他视图,请参阅本教程.

    2023-02-13 14:43 回答
  • 在我的应用程序中我使用android.support.v7.widget.SearchView并隐藏搜索图标这对我有用:

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

    2023-02-13 14:44 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有