如何在Xamarin Forms中更改Picker下划线颜色

4

我正在使用Picker控件,默认情况下,它在黑色屏幕上显示白色下划线。

enter image description here

但是,我需要将屏幕背景颜色变为白色,然后Picker下划线就完全不显示了。请查看下面的图像:

enter image description here

那么,如何更改Picker的下划线颜色呢?

这是我的Picker

 <Picker TitleColor="Black" Title="--Select--" />
2个回答

4

您可以使用自定义渲染器来实现此功能:

对于安卓设备:

[assembly: ExportRenderer(typeof(Picker), typeof(CustomPickerRenderer))]
namespace App18.Droid
{
  public class CustomPickerRenderer : PickerRenderer
   {
      private Context context;
      public CustomPickerRenderer(Context context) : base(context)
       {
        this.context = context;
       }

      protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
       {
        base.OnElementChanged(e);
        if (Control == null || e.NewElement == null) return;
        //for example ,change the line to red:
        if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            Control.BackgroundTintList = ColorStateList.ValueOf(Color.Red);
        else
            Control.Background.SetColorFilter(Color.Red, PorterDuff.Mode.SrcAtop);
       }
   }
}

iOS:

[assembly: ExportRenderer(typeof(Picker), typeof(CustomPickerRenderer))]
namespace App18.iOS
{
  public class CustomPickerRenderer : PickerRenderer
   {

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

        if (Control == null || e.NewElement == null)
            return; 
        Control.Layer.BorderWidth = 1;
        Control.Layer.BorderColor = Color.Red.ToCGColor();
    }
   }
}

如果我使用渲染器,那么我需要它适用于iOS和Android两个平台。 - R15
@Arvindraja 我已经更新了答案,你可以尝试运行它。 - Leo Zhu
在iOS中除了BorderColor我们没有其他的东西吗? - R15

0
    <StackLayout Orientation="Vertical" Spacing="0" HorizontalOptions="FillAndExpand">
                                <app1:DatePickerNoLine x:Name="datePickerScheduleDate"    Unfocused="ScheduleCriteriaChanged" VerticalOptions="Center" HorizontalOptions="FillAndExpand" FontSize="Subtitle" BackgroundColor="LightBlue"/>
                                <BoxView x:Name="BoxdatePickerScheduleDate" HeightRequest="1" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand"   Color="Black" WidthRequest="{Binding WidthRequest, Source={x:Reference datePickerScheduleDate}}" />
                            </StackLayout>

Use the custom renderer to remove the underline that comes with picker.

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

            if (e.OldElement == null)
            {
                this.Control.Text = Element.Date.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
               
                Control.Layer.BackgroundColor = Color.White.ToCGColor();

                Control.BorderStyle = UITextBorderStyle.None;
                Control.Layer.BorderColor = Color.Transparent.ToCGColor();

                Control.LeftView = new UIKit.UIView(new CGRect(0, 0, 10, 0));
                Control.LeftViewMode = UIKit.UITextFieldViewMode.Always;


                UITextField entry = Control;
                UIDatePicker picker = (UIDatePicker)entry.InputView;
                picker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels;

            }
        }

3
"Leo Zhu - MSFT" 提供的解决方案可以设置边框颜色,包括四个方向。问题是如何更改下划线颜色。 - Arun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接