Button Xamarin表单:网格内的按钮有边距。如何删除该页边距

Button Xamarin表单:网格内的按钮有边距。如何删除该页边距,button,xamarin,grid,margin,Button,Xamarin,Grid,Margin,我有一个包含一组按钮的网格。我已将网格的列间距设置为0,但按钮之间仍有边距。我以为是按钮控制。即使我将按钮上的边距设置为0,结果也是一样的 我不能使用boxView,我需要一个文本并在其上单击。问题是,android按钮具有默认的背景绘图功能,具有边距。可以使用自定义渲染器将其删除 例如: using System; using test.Droid; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: E

我有一个包含一组按钮的网格。我已将网格的列间距设置为0,但按钮之间仍有边距。我以为是按钮控制。即使我将按钮上的边距设置为0,结果也是一样的


我不能使用boxView,我需要一个文本并在其上单击。

问题是,android按钮具有默认的背景绘图功能,具有边距。可以使用自定义渲染器将其删除

例如:

using System;
using test.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Button), typeof(DefaultButtonRenderer))]
namespace test.Droid
{
    public class DefaultButtonRenderer: ButtonRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);

            // Control is Android.Widget.Button, Element is Xamarin.Forms.Button
            if (Control != null && Element != null)
            {
                // remove default background image
                Control.Background = null;

                // set background color
                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());
            }
        }
    }
}
使用系统;
使用测试机器人;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
[程序集:ExportRenderer(typeof(Button)、typeof(DefaultButtonRenderer))]
名称空间测试.Droid
{
公共类DefaultButtonRenderer:ButtonRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
//控件是Android.Widget.Button,元素是Xamarin.Forms.Button
if(Control!=null&&Element!=null)
{
//删除默认背景图像
Control.Background=null;
//设置背景色
Control.SetBackgroundColor(Element.BackgroundColor.ToAndroid());
}
}
受保护的覆盖无效OneElementPropertyChanged(对象发送方,System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(发送方,e);
如果(例如,PropertyName==“BackgroundColor”)
{
//您必须在此处再次设置背景色,因为背景色可以稍后更改。
Control.SetBackgroundColor(Element.BackgroundColor.ToAndroid());
}
}
}
}
此代码将删除按钮单击效果。如果想保持效果,必须将Control.Background设置为适当的资源。(例如:Android.Resource.Attribute.SelectableItemBackground)


或者,您可以将标签与一起使用。您可以将TapGestureRecognitor添加到任何视图。

旧线程,但我是在Android平台上获得的。将Xamarin.Forms从2.5升级到最近发布的3.1似乎解决了这个问题