SearchView自定义图标以及文字大小颜色

本文提供了一段用于定制Android中SearchView样式的代码示例,包括设置提示文本、文本颜色及大小,以及更改搜索按钮图标的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

话不多说,上代码,有需要的同学可以直接拿去用,想要寻找为什么的请查看源码。

public static void setIcon(SearchView seach, String hint, String text) {
        SearchView.SearchAutoComplete textView = (SearchView.SearchAutoComplete) seach.findViewById(R.id.search_src_text);
        textView.setTextColor(Color.WHITE);
        if (TextUtils.isEmpty(text)) {
            textView.setHint(hint);
        } else {
            textView.setText(text);
        }
        textView.setTextSize(14);
        textView.setHintTextColor(Color.WHITE);

        AppCompatImageView button = (AppCompatImageView) seach.findViewById(android.support.v7.appcompat.R.id.search_button);
        AppCompatImageView button1 = (AppCompatImageView) seach.findViewById(android.support.v7.appcompat.R.id.search_go_btn);
        AppCompatImageView button2 = (AppCompatImageView) seach.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
        button.setImageResource(R.mipmap.sousuo);
        button1.setImageResource(R.mipmap.sousuo2);
        button2.setImageResource(R.mipmap.shanchu);

        //        final int textSize = (int) (textView.getTextSize() * 1.25);
        //        Drawable mSearchHintIcon=getResources().getDrawable(R.mipmap.sousuo);
        //        mSearchHintIcon.setBounds(0, 0, textSize, textSize);
        //        final SpannableStringBuilder ssb = new SpannableStringBuilder("   ");
        //        ssb.setSpan(new ImageSpan(mSearchHintIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //        ssb.append("请输入搜索文字");
        //        textView.setHint(ssb);
    }

看了之后是不是觉得很简单呢,对了,如果要将搜索框缩小为图标直接setIconified(false)就可以了。
要是有不明白的地方欢迎留言哈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值