资料来源
searView属性及用法
https://blog.csdn.net/fan7983377/article/details/52174866
https://blog.csdn.net/airsaid/article/details/51087226
android:focusable和android:focusableInTouchMode的区别
https://blog.csdn.net/csdn_susan/article/details/46651557
带圆角的searchView的实现
https://blog.csdn.net/yuanguixi/article/details/60871598
https://blog.csdn.net/csdnzouqi/article/details/53508233
自定义搜索栏
http://www.sohu.com/a/168613010_468731
一简介
SearchView是Android原生的搜索框控件,它提供了一个用户界面,用于用户搜索查询。
SearchView默认是展示一个search的icon,点击icon展开搜索框,如果你想让搜索框默认就展开,可以通过setIconifiedByDefault(false);实现。
二属性
三监听事件
SearchView的监听器
//Sets a listener to inform when the focus of the query text field changes.查询字段改变焦点时触发
svSearchCustom.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
}
});
//监听搜索栏内的动作
//submit 用户按下提交按钮的时候,触发,重写需要返回true才有效
//change 当搜索栏内字段改变的时候触发,重写返回true生效
svSearchCustom.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
填坑
1 Couldn't resolve resource @id/search_edit_frame
https://blog.csdn.net/likai_rabbit/article/details/56831195
解决方法
a用安卓自带的serchView替换V7包的searView
b,在searchView的父控件中添加属性
android:id="@+id/search_edit_frame"
2更改默认的搜索图标和文字颜色
Android SearchView 自定义SearchIcon和字体颜色大小
在21版本后,直接在layout文件中,更改searchIcon属性,可以更改搜索图标
更改默认的文字属性
//获取到TextView的ID
int id = mSearch.getContext().getResources().getIdentifier("android:id/search_src_text",null,null);
//获取到TextView的控件
TextView textView = (TextView) mSearch.findViewById(id);
//设置字体大小为14sp
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);//14sp
//设置字体颜色
textView.setTextColor(getActivity().getResources().getColor(R.color.search_txt_color));
//设置提示文字颜色
textView.setHintTextColor(getActivity().getResources().getColor(R.color.search_hint_color));
}