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) 微信:
如果有需要的朋友可发邮件并写明需要的组件和支付流水号:邮件地址 [email protected],我将以邮件方式按要求发送相关文件