如何更改Android上按钮的文本对齐(Xamarin表单)?

igsr9ssn  于 2022-12-07  发布在  Android
关注(0)|答案(4)|浏览(242)
  • 我使用Xamarin形式,
  • 在iOS上:按钮显示的文本对齐很好

align_text_iOS

  • 但在Android上:按钮的文本对齐始终显示“居中”

align_text_Android
我找不到属性更改Android上按钮的文本对齐。
在Android上,我想文本对齐的按钮是“开始”或Android的更改按钮一样的iOS按钮。
这是我的代码:

<Button 
    HeightRequest="15"
    Text="{Binding Phone2}"
    BackgroundColor="Aqua"
    TextColor="{x:Static color:BasePalette.DarkestColor}"
    HorizontalOptions="Start"
    VerticalOptions="Center" />

请支持我!
谢谢你!

jvidinwx

jvidinwx1#

你可以从字面上设置一个填充到按钮,以推动周围的文本。(边距的按钮,填充的文本)

<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"/>
4xy9mtcn

4xy9mtcn2#

您要查找的内容类似于此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); }
    }
}

Android渲染器:

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();
    }
}

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;
        }
    }
}

不要忘记在两个渲染器[assembly: ExportRenderer(typeof(ExtendedButton), typeof(ExtendedButtonRenderer))]上添加ExportRenderer属性
如有疑问,Goodluck可随时回复

e0bqpujr

e0bqpujr3#

您需要为按钮创建自定义呈现器,然后在控件的自定义中调用:

button.Gravity = GravityFlags.Left;

您将看到文本左对齐。

k3bvogb1

k3bvogb14#

我建议使用同步按钮(SfButton)。它拥有一切。虽然它可能不是免费的,但你可以对此做更多的研究。
Alhamdulilah...它对我非常有效

相关问题