Android中自定义布局并在Custom Renderer中渲染布局,将视图添加到渲染器类。
[assembly:ExportRenderer(typeof(CustomView),typeof(MyViewRenderer))]
namespace TestApp.Droid
{
class MyViewRenderer: ViewRenderer
{
Context context;
Android.Views.View view;
public MyViewRenderer(Context context) : base(context)
{
this.context = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
view = LayoutInflater.From(context).Inflate(Resource.Layout.view_layout, null);
AddView(view);
}
protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
{
base.OnLayout(changed, left, top, right, bottom);
var width = MeasureSpec.MakeMeasureSpec(right - left, MeasureSpecMode.Exactly);
var height = MeasureSpec.MakeMeasureSpec(bottom - top, MeasureSpecMode.Exactly);
view.Measure(width,height);
view.Layout(0, 0, right - left, bottom - top);
}
}
}