【问题标题】:How to change Picker underline color in Xamarin Forms如何在 Xamarin Forms 中更改 Picker 下划线颜色
【发布时间】:2019-05-06 15:24:50
【问题描述】:

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

但我需要有白色的屏幕背景颜色,然后Picker 下划线根本不显示。见下图:

那么我怎样才能改变Picker下划线颜色

这是我的Picker

 <Picker TitleColor="Black" Title="--Select--" />

【问题讨论】:

    标签: xaml xamarin xamarin.forms picker


    【解决方案1】:

    你可以使用自定义渲染器来实现它:

    安卓系统:

    [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();
        }
       }
    }
    

    【讨论】:

    • 如果我使用 rednderer,那么 iOS 和 Android 都需要它。
    • @Arvindraja 我已经更新了答案,你可以尝试运行它
    • iOS 中除了BorderColor 之外就没有别的了吗?
    【解决方案2】:
        <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;
    
                }
            }
    

    【讨论】:

    • “Leo Zhu - MSFT”的解决方案设置边框颜色,所有 4 个边。问题是如何更改下划线颜色。
    猜你喜欢
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 2018-09-11
    • 2020-11-26
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 2011-12-02
    相关资源
    最近更新 更多