【问题标题】:How can I remove Margin from a Button in Xamarin Forms XAML?如何从 Xamarin Forms XAML 中的按钮中删除边距?
【发布时间】:2017-01-09 15:58:31
【问题描述】:

我对 Xamarin 完全陌生,所以请耐心等待! 不知何故,Xamarin 为我的所有按钮添加了一个神秘的边距,我不知道如何摆脱它。

这是我的代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="RockStamp.CameraSearch_Scan">

    <StackLayout Orientation="Vertical" Padding="0" Spacing="0">
      <StackLayout Orientation="Horizontal" Padding="0" Spacing="0">
      <Button  Text="Test" HeightRequest="50" WidthRequest="60" TextColor="#333333" x:Name="btnBack" VerticalOptions="Center" HorizontalOptions="Start" ></Button>
      <Label Text="Scan a..." FontSize="20" FontAttributes="Bold" BackgroundColor="{StaticResource BlueBackColor}" TextColor="White"  VerticalOptions="Start" HorizontalOptions="FillAndExpand" />
    </StackLayout>

    <Label Text="Steady now, we try to detect your..." FontSize="16"  VerticalOptions="Start" HorizontalOptions="Start" />

    <!-- Camera Placeholder -->
    <BoxView  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="#eeeeee" ></BoxView>

    <StackLayout Orientation="Horizontal" Padding="0" Spacing="0">
      <Button Text="Cancel" TextColor="#333333" x:Name="btnCancel"  VerticalOptions="Center" HorizontalOptions="FillAndExpand" ></Button>
      <Button Text="Scan now!" TextColor="#333333" x:Name="btnScan"  VerticalOptions="Center" HorizontalOptions="FillAndExpand" ></Button>
    </StackLayout>

  </StackLayout >

</ContentPage>

这是一张图片:

您可以清楚地看到按钮周围的空间。它来自哪里 - 更重要的是:我该如何移除它?

【问题讨论】:

    标签: button xamarin xamarin.android xamarin.forms margin


    【解决方案1】:

    问题是默认按钮背景包含这个边距。您必须将BackgroundColor 设置为一种颜色,并将BorderWidthBorderRadius 手动设置为零。

    <Button
        BackgroundColor="Fuchsia"
        BorderRadius="0"
        BorderWidth="0"
        Text="Test"
        HeightRequest="50"
        WidthRequest="60"
        TextColor="#333333"
        x:Name="btnBack"
        Margin="0"
        VerticalOptions="Start"
        HorizontalOptions="Start" />
    

    【讨论】:

    • 非常感谢您的快速回答!不幸的是,这对我不起作用,按钮仍然具有相同的边距。可能是在某个地方存在另一个(全局)设置或需要更改的东西吗?此外:这样做margin是不是很奇怪?我的意思是,谁应该知道?这是非常不合逻辑的。如果我也想拥有 Borderradius / witdh 怎么办?
    • 一切都是最新的(Xamarin.Forms)吗?你清理和重建了吗?正如您在屏幕截图中看到的那样,它绝对可以正常工作(Android 6.0)。
    • 您在使用 AppCompat 吗?看来 AppCompat 可能会阻止此解决方案的工作。
    • 如果是这种情况,那么这个答案结合在forums.xamarin.com/discussion/75912/bordercolor-isnt-working 的所选答案中实现两个类对我有用。
    【解决方案2】:

    你可能已经解决了这个问题:

    我解决这个问题的方法是在 android 上创建一个渲染器来覆盖 xamarin 上的按钮。

    public class FixedMarginRenderer : ButtonRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);
    
            if (Control != null)
            {
                // remove default background image
                Control.Background = null;
                Control.SetBackgroundColor(Element.BackgroundColor.ToAndroid()); 
            }
        }
    
        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
    
            if (e.PropertyName == "BackgroundColor")
            {
                // You have to set background color here again, because the background color can be changed later.
                Control.SetBackgroundColor(Element.BackgroundColor.ToAndroid());
            }
        }
    } 
    

    【讨论】:

    • 我也用过 Control?.SetPadding(0, 0, 0, 0);在 OnElementChanged 方法中。否则,对于固定宽度和高度的按钮,默认填充可能会弄乱按钮内文本的布局。 - 来自stackoverflow.com/a/36185809/1751497
    【解决方案3】:

    只是添加到 Sven-Michael Stübe 的answer

    如果您的按钮都不需要 Margin,则在您的 PCL 中创建 Style 或自定义控件(例如:MarginLessButton)。

    【讨论】:

      【解决方案4】:

      手动设置BackgroundColorBorderWidthBorderRadius 不再起作用。但是您可以使用平台配置来删除填充。阴影也需要去掉,否则会有一些垂直边距。

      试试这样的:

      using Xamarin.Forms;
      using Xamarin.Forms.PlatformConfiguration;
      using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
      
      namespace MyControls
      {
          public class ButtonNoMargin : Xamarin.Forms.Button
          {
              public ButtonNoMargin() : base()
              {
                 this.On<Android>().SetUseDefaultPadding(false);
                 this.On<Android>().SetUseDefaultShadow(false);
              }
          }
      }
      

      欲了解更多信息,请参阅https://github.com/xamarin/Xamarin.Forms/pull/1935

      【讨论】:

        猜你喜欢
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2016-11-23
        • 2015-12-13
        • 2022-10-19
        • 2017-11-19
        • 2023-03-09
        • 1970-01-01
        相关资源
        最近更新 更多