话不多说,上代码,有需要的同学可以直接拿去用,想要寻找为什么的请查看源码。
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)就可以了。
要是有不明白的地方欢迎留言哈。