【发布时间】:2016-10-06 08:06:52
【问题描述】:
我创建了一个自定义组件,它扩展了 ContentView 和渲染器,在 Android 上渲染为 CardView。
我面临的问题是表单内容呈现在 CardView 下方。在 KitKat 上不会发生这种情况,但我认为 CardView 的实现与 Lollipop 或更新版本的不同。
将 CardView 的背景颜色设置为透明(0x00000000)会显示 CardView 下方的内容。
表单组件:
using Xamarin.Forms;
namespace CodeFest
{
public class NativeClientProfile : ContentView
{
public NativeClientProfile()
{
var grid = new Grid
{
RowDefinitions = new RowDefinitionCollection {new RowDefinition()},
ColumnDefinitions = new ColumnDefinitionCollection {new ColumnDefinition(), new ColumnDefinition()}
};
grid.Children.Add(new Label {Text = "FSP No"}, 0, 0);
grid.Children.Add(new Label {Text = "12345", HorizontalTextAlignment = TextAlignment.Center}, 1, 0);
grid.Children.Add(new Label {Text = "Risk"}, 0, 1);
grid.Children.Add(
new Label {Text = "Low", TextColor = Color.Green, HorizontalTextAlignment = TextAlignment.Center}, 1, 1);
var item = new Label
{
Text = "Foo bar",
HorizontalTextAlignment = TextAlignment.Center,
FontSize = 30,
FontAttributes = FontAttributes.Bold
};
Content = new StackLayout
{
Children =
{
item,
new Label
{
Text = "Financial Services Provider",
HorizontalTextAlignment = TextAlignment.Center
},
grid
}
};
}
}
}
自定义渲染器:
using Android.Support.V7.Widget;
using Android.Text;
using Android.Views;
using CodeFest;
using CodeFest.Droid.ComponentRenderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(NativeClientProfile), typeof(NativeClientProfileRenderer))]
namespace CodeFest.Droid.ComponentRenderers
{
class NativeClientProfileRenderer : ViewRenderer<NativeClientProfile, CardView>
{
protected override void OnElementChanged(ElementChangedEventArgs<NativeClientProfile> elementChangedEventArgs)
{
var view = new CardView(Context);
//view.SetCardBackgroundColor(0x00000000);
SetNativeControl(view);
}
}
}
我正在寻找如何在 CardView 自定义渲染器中正确渲染表单组件的示例。
【问题讨论】:
-
CardView 通常与 RecyclerView 一起使用。你想在 RecyclerView 之外使用它吗?如果它是独立视图,那么为什么需要 CardView?
标签: xamarin xamarin.android xamarin.forms