如何从Xamarin Forms XAML中的Button中删除Margin?

时间:2016-09-01 16:57:08

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

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

这是一张图片: enter image description here

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

4 个答案:

答案 0 :(得分:6)

您可能已解决此问题:

我修复此问题的方法是在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());
        }
    }
} 

答案 1 :(得分:4)

问题是默认按钮背景包含此边距。您必须将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" />

No more border

答案 2 :(得分:0)

加入Sven-MichaelStübe的answer

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

答案 3 :(得分:0)

手动设置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