简体   繁体   English

如何从 Xamarin Forms XAML 中的按钮中删除边距?

[英]How can I remove Margin from a Button in Xamarin Forms XAML?

I'm totally new to Xamarin, so please be patient!我是 Xamarin 的新手,所以请耐心等待! Somehow Xamarin adds a mysterious Margin to all my Buttons and I don't know how to get rid of it.不知何故,Xamarin 为我所有的按钮添加了一个神秘的边距,我不知道如何摆脱它。

Here is my Code:这是我的代码:

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

Here an image:这里有一个图像: 在此处输入图片说明

You can clearly see the space around the Button.您可以清楚地看到按钮周围的空间。 Where does it come from - and more important: How can I remove it?它来自哪里 - 更重要的是:我该如何删除它?

The problem is that the default button background contains this margin.问题是默认按钮背景包含此边距。 You have to set the BackgroundColor to a color and set the BorderWidth and BorderRadius to zero manually.您必须将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" />

没有边界了

You may have fixed the problem :您可能已经解决了问题:

The way I fixed this is to create a renderer on android to override the button on xamarin.我解决这个问题的方法是在 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());
        }
    }
} 

Just to add on to Sven-Michael Stübe's answer .只是为了补充 Sven-Michael Stübe 的回答

If none of your buttons require the Margin then create a Style or a custom control in your PCL (eg: MarginLessButton).如果您的按钮都不需要 Margin,则在您的 PCL 中创建一个样式或自定义控件(例如:MarginLessButton)。

Setting BackgroundColor , BorderWidth , and BorderRadius manually does not work anymore.手动设置BackgroundColorBorderWidthBorderRadius不再起作用。 But you can use platform configuration to remove the padding.但是您可以使用平台配置来删除填充。 It is also necessary to remove the shadow otherwise there will be some vertical margin.还需要去除阴影,否则会有一些垂直边距。

Try something like this:尝试这样的事情:

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

For more information, see https://github.com/xamarin/Xamarin.Forms/pull/1935有关详细信息,请参阅https://github.com/xamarin/Xamarin.Forms/pull/1935

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM