由于SearchView未提供修改文本输入光标及文字颜色属性的接口,在实际开发常有修改光标及文字颜色的需求,这时可通过反射机制来实现。具体如下:
......
import java.lang.reflect.Field;
......
mSearchView = (SearchView) findViewById(R.id.navigation_search_bar);
int searchPlateId = mSearchView.getContext().getResources()
.getIdentifier("android:id/search_plate", null, null);
View searchPlate = mSearchView.findViewById(searchPlateId);
if (searchPlate != null) {
int searchTextId = searchPlate.getContext().getResources()
.getIdentifier("android:id/search_src_text", null, null);
//文字颜色
TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
if (searchText != null) {
searchText.setTextColor(Color.WHITE);
searchText.setHintTextColor(Color.WHITE);
}
//光标颜色
try {
Field mCursorDrawableRes=TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchText, R.drawable.cursor_color);
} catch (Exception e){
}
}
......
//drawable/cursor_color.xml
android:shape="rectangle" >
阅读(6901) | 评论(0) | 转发(0) |