CustomPicker 确定和取消按钮的颜色

CustomPicker Ok and Cancel buttons' color

我在 android 项目中有这个自定义选择器 class:

 public class CustomPickerRenderer : PickerRenderer
    {
        private Context context;
        private IElementController ElementController => Element as IElementController;
        private AlertDialog _dialog;

        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;
            Control.Click += Control_Click1;
        }
        protected override void Dispose(bool disposing)
        {
            Control.Click -= Control_Click1;
            base.Dispose(disposing);
        }

        private void Control_Click1(object sender, EventArgs e)
        {
            Picker model = Element;

            var picker = new NumberPicker(Context);
            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;
            }

            var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
            layout.AddView(picker);

            ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);

            var builder = new AlertDialog.Builder(Context);
            builder.SetView(layout);
            builder.SetTitle(model.Title ?? "");

            //change the text or color here
            builder.SetNegativeButton(Html.FromHtml("<font color='#039BE5'>Cancel</font>"), (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;
            });

             //change the text or color here
            builder.SetPositiveButton(Html.FromHtml("<font color='#039BE5'>OK</font>"), (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();
        }
    }

我在我的 phone 小米 POCOPHONE F1 (Android 9) 和 2 个模拟器 (Android 8.1) 上尝试了 运行 我的项目以及 cancel 和 ok 的颜色按钮设计完美。但是当我在华为 PLE-701L 和三星 SM-T365 (Android 5.1) 上尝试 运行 项目时,按钮的颜色没有改变。

有什么建议吗?

在style.xml

中添加此样式
<style name="SpinnerDialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:popupBackground">#ff00ff</item>
    <item name="colorPrimary">#ff00ff</item>
    <item name="colorPrimaryDark">#ffff00</item>
    <item name="colorAccent">#ff0000</item>
</style>

您可以更改所有颜色,包括按钮。

你也可以使用

 <style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert">
    <item name="android:colorPrimary">#1e87f0</item>
    <item name="android:colorAccent">#1e87f0</item>
  </style>

  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#1e87f0</item>
  </style>

通过对话框API获取按钮对象,并设置按钮的文字颜色。这种方法可以个性化。需要注意一点:必须在show

之后调用

在您的自定义渲染器中,在 _dialog.Show();

下方
....
_dialog.Show();
Button btnOk = _dialog.GetButton((int)DialogInterface.ButtonPositive);
btnOk .SetTextColor(Color.Red);
Button btnCancel= _dialog.GetButton((int)DialogInterface.ButtonNegative);
btnCancel.SetTextColor(Color.Red);

这里回答了问题:

我在 link 的正确答案中添加了样式代码,它起作用了!