【问题标题】:Custom Picker Xamarin Android自定义选择器 Xamarin Android
【发布时间】:2020-12-10 15:57:57
【问题描述】:

我有一个选择器。请告诉我如何更改标题颜色、项目颜色并删除这些行

我有我的自定义选择器,我可以更改按钮的颜色 CANCEL,OK 我试图删除线条 https://forums.xamarin.com/discussion/78693/how-can-i-remove-the-picker-borders-in-forms-for-android 但它不起作用 如何改变SELECT A CAR和AUDI的颜色我不知道

【问题讨论】:

    标签: c# xamarin xamarin.forms xamarin.android custom-renderer


    【解决方案1】:

    您可以使用 Custom Renderer

    来实现它
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Android.App;
    using Android.Content;
    using Android.Graphics;
    using Android.Graphics.Drawables;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using App12.Droid;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    using Color = Android.Graphics.Color;
    using Orientation = Android.Widget.Orientation;
    
    [assembly: ExportRenderer(typeof(Picker), typeof(MyPickerRenderer))]
    namespace App12.Droid
    {
        public class MyPickerRenderer:PickerRenderer
        {
            IElementController ElementController => Element;
    
    
            public MyPickerRenderer(Context context) : base(context)
            {
             
    
            }
    
    
            private AlertDialog _dialog;
    
    
            protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
            {
                base.OnElementChanged(e);
                Control.Click += Control_Click;
    
                Control.SetHintTextColor(Android.Graphics.Color.Red);
                Control.SetSingleLine(true);
                Control.SetTypeface(null, TypefaceStyle.Bold);
                Control.Gravity = GravityFlags.Center;
    
                var gd = new GradientDrawable();
                gd.SetStroke(0, Android.Graphics.Color.Transparent);
                Control.SetBackground(gd);
    
            }
    
    
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    Control.Click -= Control_Click;
                    //var picker = (Picker)Element;
                    //picker.PropertyChanged -= Control_Click;
                }
    
    
                base.Dispose(disposing);
            }
    
            private void Control_Click(object sender, EventArgs e)
            {
                Picker model = Element;
               
                picker.SelectionDividerHeight = 0;
    
                var picker = new TextColorNumberPicker(Context);
    
                if (model.Items != null && model.Items.Any())
                {
                    // set style here
    
                    picker.MaxValue = model.Items.Count - 1;
                    picker.MinValue = 0;
    
                  
                    picker.SetDisplayedValues(model.Items.ToArray());
                    picker.WrapSelectorWheel = false;
                    picker.Value = model.SelectedIndex;
    
                }
    
    
                var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
                layout.AddView(picker);
    
                var titleView = new TextView(Context);
    
                titleView.Text = "Select a car";
                titleView.TextSize = 20;
                titleView.SetTextColor(Color.Red);
                titleView.SetBackgroundColor(Color.White);
    
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);
    
                var builder = new AlertDialog.Builder(Context);
                builder.SetView(layout);
    
                builder.SetTitle(model.Title ?? "");
    
                builder.SetCustomTitle(titleView);
                builder.SetNegativeButton("Cancel  ", (s, a) =>
                {
                    ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                    // It is possible for the Content of the Page to be changed when Focus is changed.
                    // In this case, we'll lose our Control.
                    Control?.ClearFocus();
                    _dialog = null;
                });
                builder.SetPositiveButton("Ok ", (s, a) =>
                {
                    ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
                    // It is possible for the Content of the Page to be changed on SelectedIndexChanged.
                    // In this case, the Element & Control will no longer exist.
                    if (Element != null)
                    {
                        if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
                            Control.Text = model.Items[Element.SelectedIndex];
                        ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                        // It is also possible for the Content of the Page to be changed when Focus is changed.
                        // In this case, we'll lose our Control.
                        Control?.ClearFocus();
                    }
                    _dialog = null;
                });
    
    
                _dialog = builder.Create();
                _dialog.DismissEvent += (ssender, args) =>
                {
                    ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                };
                _dialog.Show();
    
                Android.Widget.Button btnOk = _dialog.GetButton((int)Android.Content.DialogButtonType.Positive);
                btnOk.SetTextColor(Android.Graphics.Color.Blue);
    
                Android.Widget.Button btnCancel = _dialog.GetButton((int)Android.Content.DialogButtonType.Positive);
                btnCancel.SetTextColor(Android.Graphics.Color.Gray);
            }
    
    
    
            
        }
    
    
        public class TextColorNumberPicker : NumberPicker
        {
            public TextColorNumberPicker(Context context) : base(context)
            {
    
            }
    
            public override void AddView(Android.Views.View child, int index, ViewGroup.LayoutParams @params)
            {
                base.AddView(child, index, @params);
                UpdateView(child);
            }
    
            public void UpdateView(Android.Views.View view)
            {
                if (view is EditText)
                {
    
    
                    ((EditText)view).SetTextColor(Color.Red);  // set item text color 
    
    
                }
            }
        }
    
    }
    

    注意:确保项目的目标框架是最新的稳定版本(Android Q)。

    【讨论】:

    • 我不明白。我有错误。它说函数不是 ovveride。但是你写了 ovveride 我编辑并显示错误
    • 它有效。你能再帮我一次吗?你能说我必须添加到自定义选择器以添加图像吗?我在上面添加图片
    【解决方案2】:

    覆盖Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer 而不是Xamarin.Forms.Platform.Android.PickerRenderer

    public class CustomPickerRenderer
        : Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
    {
        ...
    }
    

    参考:https://www.damirscorner.com/blog/posts/20201204-CustomPickerRendererOnAndroid.html

    【讨论】:

      猜你喜欢
      • 2018-12-25
      • 2022-01-02
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 2012-08-25
      相关资源
      最近更新 更多