在我的Xamarin表单应用程序中,我需要为iOS中的Entry控件设置一个顶部填充。我为Entry创建了渲染器,但只有我能够设置左右填充。请帮我。以下是我的入门渲染器
public class CustomRenderer: EntryRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged (e);
if (Control != null) {
Control.LeftView = new UIView (new CGRect (0, 0, 15, 0));
Control.LeftViewMode = UITextFieldViewMode.Always;
}
}
}
答案 0 :(得分:5)
Entry
的高度
UITextField
的底部对齐
在此示例中,我使用RelativeLayout
将填充条目的高度设置为50 Constraint.Constant(50)
。
using System;
using Xamarin.Forms;
namespace CustomEntrySample
{
public class EntryWithTopPadding : Entry
{
}
public class App : Application
{
public App()
{
var normalEntry = new Entry
{
Text = "This Entry Has Normal Padding",
BackgroundColor = Color.Lime
};
var paddedEntry = new EntryWithTopPadding
{
Text = "This Entry Has Extra Top Padding",
BackgroundColor = Color.Aqua
};
var mainLayout = new RelativeLayout();
mainLayout.Children.Add(
normalEntry,
Constraint.Constant(0),
Constraint.RelativeToParent(parent => parent.Y + 10),
Constraint.RelativeToParent(parent => parent.Width)
);
mainLayout.Children.Add(
paddedEntry,
Constraint.Constant(0),
Constraint.RelativeToView(normalEntry, (parent, view) => view.Y + view.Height + 10),
Constraint.RelativeToParent(parent => parent.Width),
Constraint.Constant(50)
);
MainPage = new NavigationPage(
new ContentPage
{
Title = "Title",
Content = mainLayout,
Padding = new Thickness(10, 0, 10, 0)
}
);
}
}
}
将VerticalAlignment
的{{1}}属性设置为UITextField
UIControlContentVerticalAlignment.Bottom
答案 1 :(得分:0)
使用此方法为输入单元格设置填充:
IOS中的填充:
Control.LeftView = new UIView(new CGRect(0,15,0,0));
Control.LeftViewMode = UITextFieldViewMode.Always;
Control.RightView = new UIView(new CGRect(0,10, 0, 0));
Control.RightViewMode = UITextFieldViewMode.Always;
在Android中填充:
Control.SetPadding(0, 10, 0, 0);
因此,您可以设置该值以使文本从特定位置开始。