在Android自定义渲染器代码中添加底线?

时间:2018-12-11 11:46:46

标签: xamarin.forms datepicker custom-renderer gradientdrawable

我知道默认的DatePicker已经有一个底线,但是我正试图在自定义渲染器代码中为DatePicker添加一个底线(出于某种目的)。

我可以通过GradientDrawable来设置myGradientDrawable.SetStroke(3, myColor);对象的完整边框,但是我不知道如何仅添加底线,以便任何人都可以帮助我?

1 个答案:

答案 0 :(得分:0)

尝试一下:

public class CustomPickerRenderer : PickerRenderer
{
    public CustomPickerRenderer(Context context) : base(context)
    {
    }

    private AlertDialog alert;
    private CustomPicker element;
    private int selectedIndex;

    public LayerDrawable AddPickerStyles(string imagePath)
    {
        ColorDrawable borderColorDrawable = new ColorDrawable(Xamarin.Forms.Color.FromHex("#43addf").ToAndroid());
        ColorDrawable backgroundColorDrawable = new ColorDrawable(Xamarin.Forms.Color.FromHex("#7e1b80").ToAndroid());

        Drawable[] drawables = new Drawable[]
        {
            borderColorDrawable, backgroundColorDrawable
        };

        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        layerDrawable.SetLayerInset(1, 0, 0, 0, 5);

        return layerDrawable;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);

        element = (CustomPicker)this.Element;

        if (Control != null && this.Element != null)
        {
            Control.Background = AddPickerStyles(element.Image);
        }
    } 

}