Round Corner Gridlayout Xamarin Android

问题描述 投票:1回答:1

我想创建RoundCorner GridLayout。我在Xamarin的Gridlayot控件中添加了圆角,但在背面这样的Gridlayout BackgroundColor显示了。请帮助我解决问题。GridLayout after Drawable Add

Xamarin.Forms Xaml代码

xmlns:local="clr-namespace:XamainControl.Renderer;assembly=XamainKcsControl"
<Grid BackgroundColor="Purple" WidthRequest="300">
         <Grid.RowDefinitions>
  <RowDefinition Height="Auto"/>      
</Grid.RowDefinitions>
<local:KcsGrid Grid.Row="0"
               BorderColor="Blue"
               BackgroundColor="Yellow"
               BorderRadius="20"
               BorderWidth="10" Padding="15" >
  <Label Text="Customization Page"
         FontSize="25"
         TextColor="Red"
         VerticalOptions="CenterAndExpand"
         HorizontalOptions="CenterAndExpand"/>
</local:KcsGrid>
</Grid>

自定义网格属性创建Xamarin.Forms

 public class KcsGrid : Grid
{
    #region Constructor
    public KcsGrid() : base()
    {
        this.Margin = new Thickness(0, 0, 0, -6);
    }
    #endregion

    #region Properties   

    public static BindableProperty BorderColorProperty = BindableProperty.Create<KcsGrid, Color>(o => o.BorderColor, Color.Transparent);

    public Color BorderColor
    {
        get { return (Color)GetValue(BorderColorProperty); }
        set { SetValue(BorderColorProperty, value); }
    }

    public static BindableProperty BorderWidthProperty = BindableProperty.Create<KcsGrid, float>(o => o.BorderWidth, 0);

    public float BorderWidth
    {
        get { return (float)GetValue(BorderWidthProperty); }
        set { SetValue(BorderWidthProperty, value); }
    }

    public static BindableProperty BorderRadiusProperty = BindableProperty.Create<KcsGrid, float>(o => o.BorderRadius, 0);

    public float BorderRadius
    {
        get { return (float)GetValue(BorderRadiusProperty); }
        set { SetValue(BorderRadiusProperty, value); }
    }


    public static BindableProperty WidthValueProperty = BindableProperty.Create<KcsGrid, int>(o => o.WidthValue,100);

    public int WidthValue
    {
        get { return (int)GetValue(WidthValueProperty); }
        set { SetValue(WidthValueProperty, value); }
    }

    public static BindableProperty HeightValueProperty = BindableProperty.Create<KcsGrid, int>(o => o.HeightValue,50);

    public int HeightValue
    {
        get { return (int)GetValue(HeightValueProperty); }
        set { SetValue(HeightValueProperty, value); }
    }

    #endregion
}

Xamarin.Android代码

命名空间XamainKcsControl.Droid.Renderer{

public class CustomGridRender : ViewRenderer<KcsGrid,GridLayout>
{
    private BorderRenderer _renderer;
    GridLayout Cusgd;
    protected override void OnElementChanged(ElementChangedEventArgs<KcsGrid> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement != null || this.Element == null)
            return;                                     
        Cusgd = new GridLayout(Context);
        if (Control == null)
        {              
            var curgd = Element as KcsGrid;
            SetNativeControl(Cusgd);
            UpdateBackground(curgd);
            //UpdateMeasurelayout(curgd);
        }                               
    }    

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (Element == null)
            return;
        var Gd = Element as KcsGrid;

        if (e.PropertyName == KcsGrid.BorderColorProperty.PropertyName || e.PropertyName == KcsGrid.BorderWidthProperty.PropertyName || e.PropertyName == KcsGrid.BorderRadiusProperty.PropertyName)
        {
            UpdateBackground(Gd);
        }

        if (e.PropertyName == KcsGrid.WidthValueProperty.PropertyName || e.PropertyName == KcsGrid.HeightValueProperty.PropertyName)
        {
            UpdateMeasurelayout(Gd);
        }
        //SetNativeControl(Cusgd);
    }

    void UpdateMeasurelayout(KcsGrid Gd)
    {
        GridLayout.LayoutParams Gdparm = new GridLayout.LayoutParams();
        Gdparm.Width = (int)Forms.Context.ToPixels(Gd.WidthValue);
        Gdparm.Height = (int)Forms.Context.ToPixels(Gd.HeightValue);            
        Cusgd.LayoutParameters = Gdparm;
        //SetNativeControl(Cusgd);            
    }

    void UpdateBackground(KcsGrid Gd)
    {
        if (_renderer != null)
        {
            _renderer.Dispose();
            _renderer = null;
        }
        _renderer = new BorderRenderer();
        Cusgd.Background = _renderer.GetBorderBackground(Gd.BorderColor,Gd.BackgroundColor, Gd.BorderWidth, Gd.BorderRadius);            
    }
}

}

渐变可绘制

  public Drawable GetBorderBackground(Color borderColor, Color backgroundColor, float borderWidth, float borderRadius)
    {
        if (_background != null)
        {
            _background.Dispose();
            _background = null;
        }
        borderWidth = borderWidth > 0 ? borderWidth : 0;
        borderRadius = borderRadius > 0 ? borderRadius : 0;
        borderColor = borderColor != Color.Default ? borderColor : Color.Transparent;
        backgroundColor = backgroundColor != Color.Default ? backgroundColor : Color.Transparent;

        var strokeWidth = Xamarin.Forms.Forms.Context.ToPixels(borderWidth);
        var radius = Xamarin.Forms.Forms.Context.ToPixels(borderRadius);
        _background = new GradientDrawable();
        _background.SetColor(backgroundColor.ToAndroid());
        if (radius > 0)
            _background.SetCornerRadius(radius);
        if (borderColor != Color.Transparent && strokeWidth > 0)
        {
            _background.SetStroke((int)strokeWidth, borderColor.ToAndroid());
        }

        return _background;
    }
xamarin xamarin.ios xamarin.android xamarin.forms xamarin.forms.labs
1个回答
0
投票

我知道我来晚了,但是我认为这个插件真的很好,我想分享这些信息。

https://www.nuget.org/packages/Xamarin.Forms.PancakeView/1.3.7?_src=template

© www.soinside.com 2019 - 2024. All rights reserved.