Xamarin.ToolKit第二波先来看下效果

一 圆角按钮

xamarin.froms提供的标准button按钮设置了圆角和边框,都没有明显圆角样式,于是乎自己重写了渲染类。道理吧就是重写ButtonRenderer,以下代码是安卓的实现,ios也有实现且非常简单这里就不贴出来了。

 public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{ var selected = new GradientDrawable();//创建drawable
var selectedColor = new MyGraphic.Color(Element.BackgroundColor.ToAndroid().ToArgb() + );
selected.SetColor(selectedColor);
selected.SetCornerRadius(Element.BorderRadius*);
selected.SetStroke((int)Element.BorderWidth, Element.BorderColor.ToAndroid()); var unSelected = new GradientDrawable();//创建drawable
unSelected.SetColor(Element.BackgroundColor.ToAndroid());
unSelected.SetCornerRadius(Element.BorderRadius * );
unSelected.SetStroke((int)Element.BorderWidth, Element.BorderColor.ToAndroid()); var drawable = new StateListDrawable();
drawable.AddState(new int[] { MyAndroid.Resource.Attribute.StatePressed },
selected);
drawable.AddState(new int[] { -MyAndroid.Resource.Attribute.StatePressed },
unSelected); Control.SetBackgroundDrawable(drawable);
}
}
}

二 带图标的输入框

xamarin.froms提供的标准Entry,在安卓里面显示出来很丑只有一个下边框,且没有什么可定义的样式设置,于是乎自己重写定义一个Entry,让他具有边框,边框颜色,还有一点圆角,也可以设置左边和右边的图标

下面同样是安卓的实现代码

1 IconEntry类

  public class IconEntry : Entry
{
#region 静态属性
public static readonly BindableProperty DrawLeftProperty;
public static readonly BindableProperty DrawRightProperty;
public static readonly BindableProperty BorderColorProperty;
#endregion #region 属性 /// <summary>
/// 左边图标文件
/// </summary>
public string DrawLeft
{
get { return this.GetValue(DrawLeftProperty).ToString(); }
set { SetValue(DrawLeftProperty, value); }
} /// <summary>
/// 右边图标文件
/// </summary>
public string DrawRight
{
get { return this.GetValue(DrawRightProperty).ToString(); }
set { SetValue(DrawRightProperty, value); }
} /// <summary>
/// 边框颜色
/// </summary>
public Color BorderColor
{
get { return (Color)GetValue(BorderColorProperty); }
set { SetValue(BorderColorProperty, value); }
} #endregion #region 构造
static IconEntry()
{
DrawLeftProperty = BindableProperty.Create<IconEntry, string>(
p => p.DrawLeft,
string.Empty,
BindingMode.OneWay,
propertyChanged: DrawLeftChangedHandler); DrawRightProperty = BindableProperty.Create<IconEntry, string>(
p => p.DrawRight,
string.Empty,
BindingMode.OneWay,
propertyChanged: DrawRightChangedHandler); BorderColorProperty = BindableProperty.Create<IconEntry, Color>(
p => p.BorderColor,
Color.Transparent,
BindingMode.OneWay,
propertyChanged: BorderColorChangedHandler);
}
#endregion #region 方法 #region DrawLeft变化处理
private static void DrawLeftChangedHandler(BindableObject bindable,
string oldValue, string newValue)
{
if (!oldValue.Equals(newValue))
{
var pageHead = (IconEntry)bindable;
pageHead.DrawLeft = newValue;
}
}
#endregion #region DrawRight变化处理
private static void DrawRightChangedHandler(BindableObject bindable,
string oldValue, string newValue)
{
if (!oldValue.Equals(newValue))
{
var pageHead = (IconEntry)bindable;
pageHead.DrawRight = newValue;
}
}
#endregion #region BorderColor变化处理
private static void BorderColorChangedHandler(BindableObject bindable,
Color oldValue, Color newValue)
{
if (!oldValue.Equals(newValue))
{
var pageHead = (IconEntry)bindable;
pageHead.BorderColor = newValue;
}
}
#endregion #endregion
}

2 IconEntryRenderer类

 public class IconEntryRenderer : EntryRenderer
{
private Bitmap LeftBitmap
{
get
{
if (Element is IconEntry)
{
var logEntry = (Element as IconEntry);
if (!string.IsNullOrEmpty(logEntry.DrawLeft))
{
var resStr = logEntry.DrawLeft;
if (resStr.Contains(".png"))
{
resStr = resStr.Replace(".png", "");
}
var id = Context.Resources.GetIdentifier(resStr,
"drawable",
Context.PackageName);
return BitmapFactory.DecodeResource(Resources, id);
}
}
return null;
}
} private Bitmap RightBitmap
{
get
{
if (Element is IconEntry)
{
var logEntry = (Element as IconEntry);
if (!string.IsNullOrEmpty(logEntry.DrawRight))
{
var resStr = logEntry.DrawRight;
if (resStr.Contains(".png"))
{
resStr = resStr.Replace(".png", "");
}
var id = Context.Resources.GetIdentifier(resStr,
"drawable",
Context.PackageName);
return BitmapFactory.DecodeResource(Resources, id);
}
}
return null;
}
} protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{ var unSelected = new GradientDrawable();//创建drawable
unSelected.SetCornerRadius();
unSelected.SetStroke(, (Element as IconEntry).BorderColor.ToAndroid());
unSelected.SetShape(ShapeType.Rectangle); var padLeft = ;
if (LeftBitmap != null)
{
padLeft += LeftBitmap.Width;
} var padRight = ;
if (RightBitmap != null)
{
padRight += RightBitmap.Width;
} Control.SetPadding(padLeft, , padRight, );
Control.SetTextColor(Element.TextColor.ToAndroid());
Control.SetBackgroundColor(Element.BackgroundColor.ToAndroid());
Control.SetBackgroundDrawable(unSelected);
Control.Gravity = GravityFlags.CenterVertical; }
} protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
if (LeftBitmap != null)
{
canvas.DrawBitmap(LeftBitmap, 20f, (Height - LeftBitmap.Height) / 2f, new Paint());
} if (RightBitmap != null)
{
canvas.DrawBitmap(RightBitmap, Width-RightBitmap.Width - 20f, (Height - RightBitmap.Height) / 2f, new Paint());
}
} }

三 同样如有朋友喜欢也可以付费支持下

1)支付宝:                                                                           2) 微信:

                   

如果有需要的朋友可发邮件并写明需要的组件和支付流水号:邮件地址 2543856397@qq.com,我将以邮件方式按要求发送相关文件

最新文章

  1. iOS 学习 - 23 加载本地 txt 文件, NSMutableParagraphStyle 段落格式,缩放动画,字体间距
  2. 修改PHP 加载loaded configuration file 目录
  3. java 7中文件的复制移动
  4. C#调试器导航
  5. http://blog.csdn.net/shawnkong/article/details/52045894
  6. HTTP Error 503. The service is unavailable
  7. Getting Started With Hazelcast 读书笔记(第四章)
  8. 涨姿势!手机端的META你知道多少?
  9. 简单介绍Javascript匿名函数和面向对象编程
  10. [转]AndroidManifest.xml文件详解
  11. Java中的blank final
  12. PreparedStatement的用法
  13. 24种设计模式--观察者模式【Observer Pattern】
  14. extjs之TypeError: d.read is not a function解决方案
  15. switch语句:适用于一个条件有多个分支的情况---分支语句
  16. ionic打包项目,运行时报错A problem occurred configuring root project &#39;android&#39;。。。
  17. Java学习笔记之字符串常用方法
  18. django获取ajax的post复杂对象
  19. VC++信息安全编程(13)Windows2000/xp/vista/7磁盘扇区读写技术
  20. 高通平台如何使用QPST抓DUMP

热门文章

  1. C#中BASE64和图片相互转换
  2. Linq左关联 右关联 内关联
  3. javascript中的__proto__和prototype
  4. IM 融云 之 列表中显示聊天用户名称
  5. runat=&quot;server&quot;
  6. Android组件生命周期(一)
  7. excel计算后列填充
  8. Intent的属性及Intent-filter配置——Component属性
  9. thinkphp 配置项总结
  10. bat-bat-bat (重要的事情说三遍)