android r.id 自定义,Android SearchView的自定义
SearchView是Android原生的控件,功能较全,简单修改代码可实现自定义的效果。首先初始化一下控件SearchView mSearchView= (SearchView)findViewById(R.id.search_view);1, searchView有个默认“关闭”状态,点击一下放大镜????图标可以展开。如果想直接显示这个展示状态,可以这样写:(我就不上gif了,懒……)mSe
SearchView是Android原生的控件,功能较全,简单修改代码可实现自定义的效果。
首先初始化一下控件
SearchView mSearchView= (SearchView)findViewById(R.id.search_view);
1, searchView有个默认“关闭”状态,点击一下放大镜🔍图标可以展开。如果想直接显示这个展示状态,可以这样写:(我就不上gif了,懒……)
mSearchView.setIconifiedByDefault(false);
2, 搜索文字下方有个下划线,如果想去掉下划线,可以把下划线的颜色改成与背景颜色一致,这样就能出来取消下划线的状态了。
其中R.id.search_plate是searchView源码中下划线的id,所以必须写正确了
View underline =mSearchView.findViewById(R.id.search_plate);
underline.setBackgroundColor(ContextCompat.getColor(this,R.color.white));
3, SearchView前面有一个默认自带的放大镜图标,如图,
就是这个放大镜图标
如果想取消,可以这么写:
ImageView searchViewIcon = (ImageView)mSearchView.findViewById(R.id.search_mag_icon);
ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);
4,修改键入的文字字体大小、颜色和hint的字体颜色
final EditText editText = (EditText)mSearchView.findViewById(R.id.search_src_text);
editText.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources()
.getDimension(R.dimen.np_search_text_size));
editText.setTextColor(ContextCompat.getColor(this,R.color.nb_text_primary));
editText.setHintTextColor(ContextCompat.getColor(this,R.color.nb_text_quaternary));
5,当点击软键盘上的”搜索“的时候,自动触发对已经输入文字的搜索:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//搜索逻辑
}
return true;
}
});
6,自定义searchView布局最右侧”❌“号图标:
ImageView closeViewIcon = (ImageView)mSearchView.findViewById(R.id.search_close_btn);
closeViewIcon.setImageDrawable(ContextCompat
.getDrawable(this,R.drawable.ic_search_close));```
7,自定义光标颜色:
try {
Field fCursorDrawableRes = TextView.class
.getDeclaredField("mCursorDrawableRes");
fCursorDrawableRes.setAccessible(true);
//此处的editText是上文第4条中的editText
int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);
Field fEditor = TextView.class.getDeclaredField("mEditor");
fEditor.setAccessible(true);
//此处的editText是上文第4条中的editText
Object editor = fEditor.get(editText);
Class> clazz = editor.getClass();
Field fCursorDrawable =
clazz.getDeclaredField("mCursorDrawable");
fCursorDrawable.setAccessible(true);
if (mCursorDrawableRes <= 0) {
return;
}
Drawable cursorDrawable =
ContextCompat.getDrawable(mSearchView.getContext(), mCursorDrawableRes);
if (cursorDrawable == null) {
return;
}
Drawable tintDrawable = DrawableCompat.wrap(cursorDrawable);
DrawableCompat.setTintList(tintDrawable,
//此处的R.color.blue_primary就是你自定义的颜色了
ColorStateList.valueOf(
ContextCompat.getColor(this, R.color.blue_primary)));
Drawable[] drawables
= new Drawable[] {tintDrawable, tintDrawable};
fCursorDrawable.set(editor, drawables);
} catch (Throwable ignored) {
}
还有一些自定义的功能,网上一搜一堆,我就不在这里列举了~~
~~~~~~~~~~~~~~~~~~~~~
请关注我的微信公众号~
![](http://upload-images.jianshu.io/upload_images/1857762-26c749d6ddc1e4da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
更多推荐
所有评论(0)