Lek*_*s G 5 xamarin.ios xamarin.android xamarin xamarin.forms
我在 Android 上找不到按钮的属性更改文本对齐方式。
在 Android 上,我希望按钮的文本对齐是“开始”或 Android 的更改按钮与 iOS 的按钮相同。
这是我的代码:
<Button
HeightRequest="15"
Text="{Binding Phone2}"
BackgroundColor="Aqua"
TextColor="{x:Static color:BasePalette.DarkestColor}"
HorizontalOptions="Start"
VerticalOptions="Center" />
Run Code Online (Sandbox Code Playgroud)
请支持我!
谢谢!
实际上,您只需为按钮设置一个填充即可推动文本。(按钮的边距,文本的内边距)
<Button Text="Log In"
Margin="0,15,0,30"
FontFamily="ButtonFont"
Padding="0,0,0,5"
FontSize="20"
BackgroundColor="#2c2c2c"
TextColor="#ffffff"
BorderRadius="20"
BorderWidth="2"
BorderColor="Aqua"
Grid.Column="1"/>
Run Code Online (Sandbox Code Playgroud)
您正在寻找的东西类似于XLab Control
public class ExtendedButton : Button
{
/// <summary>
/// Bindable property for button content vertical alignment.
/// </summary>
public static readonly BindableProperty VerticalContentAlignmentProperty =
BindableProperty.Create<ExtendedButton, TextAlignment>(
p => p.VerticalContentAlignment, TextAlignment.Center);
/// <summary>
/// Bindable property for button content horizontal alignment.
/// </summary>
public static readonly BindableProperty HorizontalContentAlignmentProperty =
BindableProperty.Create<ExtendedButton, TextAlignment>(
p => p.HorizontalContentAlignment, TextAlignment.Center);
/// <summary>
/// Gets or sets the content vertical alignment.
/// </summary>
public TextAlignment VerticalContentAlignment
{
get { return this.GetValue<TextAlignment>(VerticalContentAlignmentProperty); }
set { this.SetValue(VerticalContentAlignmentProperty, value); }
}
/// <summary>
/// Gets or sets the content horizontal alignment.
/// </summary>
public TextAlignment HorizontalContentAlignment
{
get { return this.GetValue<TextAlignment>(HorizontalContentAlignmentProperty); }
set { this.SetValue(HorizontalContentAlignmentProperty, value); }
}
}
Run Code Online (Sandbox Code Playgroud)
安卓渲染器:
public class ExtendedButtonRenderer : ButtonRenderer
{
/// <summary>
/// Called when [element changed].
/// </summary>
/// <param name="e">The e.</param>
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
UpdateAlignment();
UpdateFont();
}
/// <summary>
/// Handles the <see cref="E:ElementPropertyChanged" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="PropertyChangedEventArgs"/> instance containing the event data.</param>
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == ExtendedButton.VerticalContentAlignmentProperty.PropertyName ||
e.PropertyName == ExtendedButton.HorizontalContentAlignmentProperty.PropertyName)
{
UpdateAlignment();
}
else if (e.PropertyName == Button.FontProperty.PropertyName)
{
UpdateFont();
}
base.OnElementPropertyChanged(sender, e);
}
/// <summary>
/// Updates the font
/// </summary>
private void UpdateFont()
{
Control.Typeface = Element.Font.ToExtendedTypeface(Context);
}
/// <summary>
/// Sets the alignment.
/// </summary>
private void UpdateAlignment()
{
var element = this.Element as ExtendedButton;
if (element == null || this.Control == null)
{
return;
}
this.Control.Gravity = element.VerticalContentAlignment.ToDroidVerticalGravity() |
element.HorizontalContentAlignment.ToDroidHorizontalGravity();
}
}
Run Code Online (Sandbox Code Playgroud)
iOS 渲染器:
public class ExtendedButtonRenderer : ButtonRenderer
{
/// <summary>
/// Called when [element changed].
/// </summary>
/// <param name="e">The e.</param>
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
var element = this.Element;
if (element == null || this.Control == null)
{
return;
}
this.Control.VerticalAlignment = this.Element.VerticalContentAlignment.ToContentVerticalAlignment();
this.Control.HorizontalAlignment = this.Element.HorizontalContentAlignment.ToContentHorizontalAlignment();
}
/// <summary>
/// Handles the <see cref="E:ElementPropertyChanged" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="PropertyChangedEventArgs"/> instance containing the event data.</param>
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "VerticalContentAlignment":
this.Control.VerticalAlignment = this.Element.VerticalContentAlignment.ToContentVerticalAlignment();
break;
case "HorizontalContentAlignment":
this.Control.HorizontalAlignment = this.Element.HorizontalContentAlignment.ToContentHorizontalAlignment();
break;
default:
base.OnElementPropertyChanged(sender, e);
break;
}
}
/// <summary>
/// Gets the element.
/// </summary>
/// <value>The element.</value>
public new ExtendedButton Element
{
get
{
return base.Element as ExtendedButton;
}
}
}
Run Code Online (Sandbox Code Playgroud)
ExportRenderer不要忘记在两个渲染器上添加属性[assembly: ExportRenderer(typeof(ExtendedButton), typeof(ExtendedButtonRenderer))]
如有疑问,Goodluck 请随时回复
| 归档时间: |
|
| 查看次数: |
13645 次 |
| 最近记录: |