xamarin MAUI中的DatePicker全屏显示,并在Android中与左上角对齐

bttbmeg0  于 2023-09-28  发布在  Android
关注(0)|答案(1)|浏览(131)

你好。我正在开发一个MAUI应用程序,其中我必须使用无边框的自定义日期选择器。
但是,它出现在屏幕的角落,而不是像它应该的那样出现在中间。我现在用的是.Net 7。
下面是我的代码示例。

public class BorderLessDatePicker : DatePicker
{

        public static readonly BindableProperty TextAlignmentProperty = BindableProperty.Create("TextAlignment", typeof(PickerTextAlignment), typeof(BorderLessDatePicker), (PickerTextAlignment)0, BindingMode.TwoWay);
        public PickerTextAlignment TextAlignment
        {
            get { return (PickerTextAlignment)GetValue(TextAlignmentProperty); }
            set { SetValue(TextAlignmentProperty, value); }
        }

        public enum PickerTextAlignment
        {
            Center,
            Right,
            Left
        }
}
public class BorderLessDatePickerMapper 
{

        private static BorderLessDatePicker viewCast;

        public static void Map(IDatePickerHandler handler, IDatePicker view)
        {

            if (view is BorderLessDatePicker)
            {

                viewCast = (BorderLessDatePicker)view;

                // Remove borders
                GradientDrawable gd = new GradientDrawable();
                gd.SetStroke(0, Android.Graphics.Color.LightGray);
                handler.PlatformView.SetBackground(gd);
                
                handler.PlatformView.SetPadding(0, 0, 0, 0);

                // TextAlignment for date value
                if (viewCast.TextAlignment == BorderLessDatePicker.PickerTextAlignment.Right)
                    handler.PlatformView.Gravity = Android.Views.GravityFlags.Right;
                else if (viewCast.TextAlignment == BorderLessDatePicker.PickerTextAlignment.Center)
                    handler.PlatformView.Gravity = Android.Views.GravityFlags.Center;
                else if (viewCast.TextAlignment == BorderLessDatePicker.PickerTextAlignment.Left)
                    handler.PlatformView.Gravity = Android.Views.GravityFlags.Left;
            }
        }

       
}
<controls:BorderLessDatePicker 
       Format="MM/dd/yyyy"
       TextAlignment="Right"
       MaximumDate="9/18/2023"
       VerticalOptions="Center"
       HorizontalOptions="Fill">
 </controls:BorderLessDatePicker>

在MAUIProgram.cs中,

Microsoft.Maui.Handlers.DatePickerHandler.Mapper.AppendToMapping("DatePickerCustomization", (handler, view) =>
{
            if (view is BorderLessDatePicker)
            {
                BorderLessDatePickerMapper.Map(handler, view);
            }
});

提前谢谢你。

omvjsjqw

omvjsjqw1#

我已经找到了这个问题的根本原因,结果是一些与我想象的不同的东西。
我有一个定制的Datepicker在这里和风格是这样的。

<style name="MainTheme.PATH.Base" parent="Theme.MaterialComponents.DayNight">
    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
 </style>

现在,如果您正在扩展“材质”主题,则应使用以下命令对其进行自定义。

<item name="materialCalendarTheme">@style/AppCompatDialogStyle</item>

也代替了

<style name="AppCompatDialogStyle" parent="android:Widget.Material.DatePicker">
 </style>

你应该使用

<style name="AppCompatDialogStyle" parent="@style/ThemeOverlay.MaterialComponents.MaterialCalendar">
  </style>

谢谢.

相关问题