anroid下SearchView样式个人感觉很土,这边将它改成类iOS风格。包括自定义图标,SearchView中EditText的下划线、边框及文本样式处理.
1、添加render,这边由pcl过来,故绑定的是SearchBar,可自定义。
[assembly: ExportRenderer(typeof(SearchBar), typeof(SearchBarRenderer_Droid))]
namespace StoneApp.Droid.Renderers
{public class SearchBarRenderer_Droid : SearchBarRenderer{protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e){base.OnElementChanged(e);//读取为SearchViewvar sv = (SearchView)Control;}}
}
2、icon替换
//search_mag_icon为android中默认icon的id
int svIconId = Context.Resources.GetIdentifier("android:id/search_mag_icon", null, null);var icon = sv.FindViewById(svIconId);if (icon != null){//读取为ImageViewvar iv = (icon as ImageView);//替换iv.SetImageResource(Resource.Drawable.searchicon);//通过padding控制图片大小iv.SetPadding(12, 12, 12, 12);}
3、搜索边框处理
//search_edit_frame为编辑区域id,实际上icon也包括其中
var frameId = Context.Resources.GetIdentifier("android:id/search_edit_frame", null, null);var frame = sv.FindViewById(frameId);if (frame != null){var fv = (frame as Android.Views.View);//读取resource下的自定义shape的xml文件,通过背景设置边框fv.SetBackgroundResource(Resource.Drawable.search_border);}
shape配置信息:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#ffffff"></solid><!--边框圆角设置--><cornersandroid:topLeftRadius="3dp"android:topRightRadius="3dp"android:bottomLeftRadius="3dp"android:bottomRightRadius="3dp"></corners><!--padding--><padding android:left="1dip" android:top="1dip" android:right="1dip" android:bottom="1dip"></padding><!--边框颜色--><stroke android:width="1dip" android:color="#7cb0d9" ></stroke>
</shape>
4、字体设置
//search_src_text为android默认输入文本id
var editTextId = Context.Resources.GetIdentifier("android:id/search_src_text", null, null);var editText = sv.FindViewById(editTextId);if(editText != null){var ev = (editText as EditText);//字体颜色ev.SetTextColor(Android.Graphics.Color.ParseColor("#999999"));//字体大小ev.SetTextSize(Android.Util.ComplexUnitType.Dip, 13); }
5、移除默认下划线
var plateId = Context.Resources.GetIdentifier("android:id/search_plate", null, null);var plate = sv.FindViewById(plateId);if(plate != null){//将背景色设为白色与整个搜索框的背景色相同(plate as Android.Views.View).SetBackgroundColor(Android.Graphics.Color.ParseColor("#ffffff"));}