How to change Picker font colour and size in Xamarin forms?

后端 未结 6 1673
梦如初夏
梦如初夏 2021-01-14 17:01

I\'m new to Xamarin and I\'m currently doing a project in Xamarin Forms PCL.

Is there a way to change the font colour and size of Picker?

  

6条回答
  •  南笙
    南笙 (楼主)
    2021-01-14 17:45

    For changing typeface, size, underline, text, textcolor, alert dialog button position, button text in Android native numberpicker (xamarin form picker), you can handle it with a custom render like this:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    using Android.Graphics.Drawables;
    using Android.Graphics;
    
    [assembly: ExportRenderer(typeof(Picker), typeof(MyPickerRenderer))]
    namespace Daddy.Droid
    {
        public class MyPickerRenderer : Xamarin.Forms.Platform.Android.PickerRenderer
        {
            Typeface fontFace = null;
            private IElementController ElementController => Element as IElementController;
            private AlertDialog _dialog;
            public MyPickerRenderer(Context context) : base(context)
            {
                AutoPackage = false;
            }
            [Obsolete("This constructor is obsolete as of version 2.5. Please use PickerRenderer(Context) instead.")]
            public MyPickerRenderer()
            {
                AutoPackage = false;
            }
            protected override void OnElementChanged(ElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                if (e.NewElement == null || e.OldElement != null || Control == null)
                    return;
    
                fontFace = Typeface.CreateFromAsset(this.Context.Assets, "somefont.ttf");
    
                GradientDrawable gd = new GradientDrawable();
                gd.SetStroke(0, Android.Graphics.Color.Transparent);
                Control.SetBackground(gd);
    
                Control.TextSize = 14f;
                Control.SetTypeface(fontFace, TypefaceStyle.Normal);
    
                Control.Click += Control_Click;
            }
            protected override void Dispose(bool disposing)
            {
                Control.Click -= Control_Click;
                base.Dispose(disposing);
            }
    
            private void Control_Click(object sender, EventArgs e)
            {
                Picker model = Element;
                NumberPicker picker = new NumberPicker(Context);
    
                int count = picker.ChildCount;
                for (int i = 0; i < count; i++)
                {
                    Android.Views.View v = picker.GetChildAt(i);
                    if(v.GetType() == typeof(EditText))
                    {
                        Java.Lang.Reflect.Field  field = picker.Class.GetDeclaredField("mSelectorWheelPaint");
                        field.Accessible = true;
                        ((Paint)field.Get(picker)).SetTypeface(fontFace);
                        ((EditText)v).SetTypeface(fontFace, TypefaceStyle.Normal);
                        picker.Invalidate();   
                    }
                }
    
                if (model.Items != null && model.Items.Any())
                {
                    picker.MaxValue = model.Items.Count - 1;
                    picker.MinValue = 0;
                    picker.SetDisplayedValues(model.Items.ToArray());
                    picker.WrapSelectorWheel = false;
                    picker.DescendantFocusability = DescendantFocusability.BlockDescendants;
                    picker.Value = model.SelectedIndex;
                    picker.Visibility = ViewStates.Visible;
    
                }
    
    
                var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
                layout.Visibility = ViewStates.Visible;
                layout.AddView(picker);
    
    
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);
    
                var builder = new AlertDialog.Builder(Context); 
                builder.SetView(layout);
    
                builder.SetTitle(model.Title ?? "");
    
                builder.SetNegativeButton("Cancel", (s, a) =>
                {
                    ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                    Control?.ClearFocus();
                    _dialog = null;
                });
    
                builder.SetPositiveButton("This One", (s, a) =>
                {
                    ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
                    if (Element != null)
                    {
                        if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
                            Control.Text = model.Items[Element.SelectedIndex];
                        ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                        Control?.ClearFocus();
                    }
                    _dialog = null;
                });
    
                _dialog = builder.Create();
    
                _dialog.DismissEvent += (ssender, args) =>
                {
                    ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                };
                _dialog.Show();
    
    
                Android.Widget.Button nbutton = _dialog.GetButton((int)Android.Content.DialogButtonType.Positive);
                nbutton.SetTypeface(fontFace, TypefaceStyle.Normal);
                nbutton.SetTextColor(Android.Graphics.Color.ParseColor("#33b5e5"));
                nbutton.TextSize = 16f;
                LinearLayout layOut = (LinearLayout)nbutton.Parent;
                layOut.SetGravity(GravityFlags.CenterHorizontal);
                Android.Views.View v1 = layOut.GetChildAt(1);
                v1.Visibility = ViewStates.Gone;
    
    
                int res = Resources.GetIdentifier("alertTitle", "id", "android");
                TextView textView = (TextView)_dialog.FindViewById(res);
                textView.SetTextColor(Android.Graphics.Color.Gray);
                textView.SetTypeface(fontFace, TypefaceStyle.Normal);
                textView.Gravity = GravityFlags.Center;
    
            }
        }
    }
    

提交回复
热议问题