Xamarin.ToolKit第二波先来看下效果
一 圆角按钮
xamarin.froms提供的标准button按钮设置了圆角和边框,都没有明显圆角样式,于是乎自己重写了渲染类。道理吧就是重写ButtonRenderer,以下代码是安卓的实现,ios也有实现且非常简单这里就不贴出来了。
1 public class CustomButtonRenderer : ButtonRenderer 2 { 3 protected override void OnElementChanged(ElementChangedEventArgs<Button> e) 4 { 5 base.OnElementChanged(e); 6 if (e.OldElement == null) 7 { 8 9 var selected = new GradientDrawable();//创建drawable 10 var selectedColor = new MyGraphic.Color(Element.BackgroundColor.ToAndroid().ToArgb() + 50); 11 selected.SetColor(selectedColor); 12 selected.SetCornerRadius(Element.BorderRadius*10); 13 selected.SetStroke((int)Element.BorderWidth, Element.BorderColor.ToAndroid()); 14 15 var unSelected = new GradientDrawable();//创建drawable 16 unSelected.SetColor(Element.BackgroundColor.ToAndroid()); 17 unSelected.SetCornerRadius(Element.BorderRadius * 10); 18 unSelected.SetStroke((int)Element.BorderWidth, Element.BorderColor.ToAndroid()); 19 20 var drawable = new StateListDrawable(); 21 drawable.AddState(new int[] { MyAndroid.Resource.Attribute.StatePressed }, 22 selected); 23 drawable.AddState(new int[] { -MyAndroid.Resource.Attribute.StatePressed }, 24 unSelected); 25 26 Control.SetBackgroundDrawable(drawable); 27 } 28 } 29 }
二 带图标的输入框
xamarin.froms提供的标准Entry,在安卓里面显示出来很丑只有一个下边框,且没有什么可定义的样式设置,于是乎自己重写定义一个Entry,让他具有边框,边框颜色,还有一点圆角,也可以设置左边和右边的图标
下面同样是安卓的实现代码
1 IconEntry类
1 public class IconEntry : Entry 2 { 3 #region 静态属性 4 public static readonly BindableProperty DrawLeftProperty; 5 public static readonly BindableProperty DrawRightProperty; 6 public static readonly BindableProperty BorderColorProperty; 7 #endregion 8 9 #region 属性 10 11 /// <summary> 12 /// 左边图标文件 13 /// </summary> 14 public string DrawLeft 15 { 16 get { return this.GetValue(DrawLeftProperty).ToString(); } 17 set { SetValue(DrawLeftProperty, value); } 18 } 19 20 /// <summary> 21 /// 右边图标文件 22 /// </summary> 23 public string DrawRight 24 { 25 get { return this.GetValue(DrawRightProperty).ToString(); } 26 set { SetValue(DrawRightProperty, value); } 27 } 28 29 /// <summary> 30 /// 边框颜色 31 /// </summary> 32 public Color BorderColor 33 { 34 get { return (Color)GetValue(BorderColorProperty); } 35 set { SetValue(BorderColorProperty, value); } 36 } 37 38 #endregion 39 40 #region 构造 41 static IconEntry() 42 { 43 DrawLeftProperty = BindableProperty.Create<IconEntry, string>( 44 p => p.DrawLeft, 45 string.Empty, 46 BindingMode.OneWay, 47 propertyChanged: DrawLeftChangedHandler); 48 49 DrawRightProperty = BindableProperty.Create<IconEntry, string>( 50 p => p.DrawRight, 51 string.Empty, 52 BindingMode.OneWay, 53 propertyChanged: DrawRightChangedHandler); 54 55 BorderColorProperty = BindableProperty.Create<IconEntry, Color>( 56 p => p.BorderColor, 57 Color.Transparent, 58 BindingMode.OneWay, 59 propertyChanged: BorderColorChangedHandler); 60 } 61 #endregion 62 63 #region 方法 64 65 #region DrawLeft变化处理 66 private static void DrawLeftChangedHandler(BindableObject bindable, 67 string oldValue, string newValue) 68 { 69 if (!oldValue.Equals(newValue)) 70 { 71 var pageHead = (IconEntry)bindable; 72 pageHead.DrawLeft = newValue; 73 } 74 } 75 #endregion 76 77 #region DrawRight变化处理 78 private static void DrawRightChangedHandler(BindableObject bindable, 79 string oldValue, string newValue) 80 { 81 if (!oldValue.Equals(newValue)) 82 { 83 var pageHead = (IconEntry)bindable; 84 pageHead.DrawRight = newValue; 85 } 86 } 87 #endregion 88 89 #region BorderColor变化处理 90 private static void BorderColorChangedHandler(BindableObject bindable, 91 Color oldValue, Color newValue) 92 { 93 if (!oldValue.Equals(newValue)) 94 { 95 var pageHead = (IconEntry)bindable; 96 pageHead.BorderColor = newValue; 97 } 98 } 99 #endregion 100 101 #endregion 102 }
2 IconEntryRenderer类
1 public class IconEntryRenderer : EntryRenderer 2 { 3 private Bitmap LeftBitmap 4 { 5 get 6 { 7 if (Element is IconEntry) 8 { 9 var logEntry = (Element as IconEntry); 10 if (!string.IsNullOrEmpty(logEntry.DrawLeft)) 11 { 12 var resStr = logEntry.DrawLeft; 13 if (resStr.Contains(".png")) 14 { 15 resStr = resStr.Replace(".png", ""); 16 } 17 var id = Context.Resources.GetIdentifier(resStr, 18 "drawable", 19 Context.PackageName); 20 return BitmapFactory.DecodeResource(Resources, id); 21 } 22 } 23 return null; 24 } 25 } 26 27 private Bitmap RightBitmap 28 { 29 get 30 { 31 if (Element is IconEntry) 32 { 33 var logEntry = (Element as IconEntry); 34 if (!string.IsNullOrEmpty(logEntry.DrawRight)) 35 { 36 var resStr = logEntry.DrawRight; 37 if (resStr.Contains(".png")) 38 { 39 resStr = resStr.Replace(".png", ""); 40 } 41 var id = Context.Resources.GetIdentifier(resStr, 42 "drawable", 43 Context.PackageName); 44 return BitmapFactory.DecodeResource(Resources, id); 45 } 46 } 47 return null; 48 } 49 } 50 51 protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) 52 { 53 base.OnElementChanged(e); 54 if (e.OldElement == null) 55 { 56 57 var unSelected = new GradientDrawable();//创建drawable 58 unSelected.SetCornerRadius(8); 59 unSelected.SetStroke(1, (Element as IconEntry).BorderColor.ToAndroid()); 60 unSelected.SetShape(ShapeType.Rectangle); 61 62 var padLeft = 40; 63 if (LeftBitmap != null) 64 { 65 padLeft += LeftBitmap.Width; 66 } 67 68 var padRight = 20; 69 if (RightBitmap != null) 70 { 71 padRight += RightBitmap.Width; 72 } 73 74 75 Control.SetPadding(padLeft, 0, padRight, 0); 76 Control.SetTextColor(Element.TextColor.ToAndroid()); 77 Control.SetBackgroundColor(Element.BackgroundColor.ToAndroid()); 78 Control.SetBackgroundDrawable(unSelected); 79 Control.Gravity = GravityFlags.CenterVertical; 80 81 } 82 } 83 84 protected override void OnDraw(Canvas canvas) 85 { 86 base.OnDraw(canvas); 87 if (LeftBitmap != null) 88 { 89 canvas.DrawBitmap(LeftBitmap, 20f, (Height - LeftBitmap.Height) / 2f, new Paint()); 90 } 91 92 if (RightBitmap != null) 93 { 94 canvas.DrawBitmap(RightBitmap, Width-RightBitmap.Width - 20f, (Height - RightBitmap.Height) / 2f, new Paint()); 95 } 96 } 97 98 }
三 同样如有朋友喜欢也可以付费支持下
1)支付宝: 2) 微信:
如果有需要的朋友可发邮件并写明需要的组件和支付流水号:邮件地址 2543856397@qq.com,我将以邮件方式按要求发送相关文件