如何从 android 中的选择器对话框更改项目的文本颜色?

How can I change text color of items from a picker dialog in android?

我正在尝试使用 styles.xml 更改选择器对话框的文本颜色,我可以更改标题和按钮的颜色,但不会更改项目的颜色。我的愿望是白色背景和黑色文字,但由于这个问题,我不得不更改背景颜色才能看到这些项目。这就是我的 styles.xml 的样子:

<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="colorPrimary">#5cc7a3</item>
    <item name="colorPrimaryDark">#5cc7a3</item>
    <item name="colorAccent">#000000</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
    <item name="android:windowFullscreen">true</item>
    <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
    <item name="android:alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
    <item name="android:datePickerStyle">@style/AppCompatAlertDialogStyle</item>
    <item name="android:spinnerItemStyle">@style/AppCompatAlertDialogStyle</item>
    <item name="android:spinnerStyle">@style/AppCompatAlertDialogStyle</item>
    <item name="android:spinnerDropDownItemStyle">@style/AppCompatAlertDialogStyle</item>

</style>

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

<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <item name="android:windowNoTitle">true</item>  
    <item name="android:windowFullscreen">true</item>  
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowActionBar">true</item>
</style>

 <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Dialog.Alert">
   <!--buttons-->
  <item name="colorAccent">#000000</item>
   <!--title-->
   <item name="android:textColor">#000000</item>
   <!--text-->
   <item name="android:textColorPrimary">#000000</item>
   <!--selection list-->
   <item name="android:textColorTertiary">#000000</item>
   <!--background-->
   <item name="android:background">#5cc7a3</item>
 </style>

这是对话框现在的样子,如果背景是白色,我看不到项目:

尝试在您的主题中添加这一行:

 <item name="android:textColorPrimary">@color/Black</item> // change color according to your requirement

我可以使用我在 google 上找到的自定义渲染器来解决这个问题。对于那些正在寻找解决方案的人来说,代码是:

using System;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.Widget;
using BindablePicker;
using BindablePicker.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Color = Android.Graphics.Color;
using Orientation = Android.Widget.Orientation;

[assembly: ExportRenderer(typeof(PickerRenderer), typeof(CustomPickerRenderer))]
namespace BindablePicker.Droid
{
 public class CustomPickerRenderer : PickerRenderer//, ViewRenderer<Picker, EditText>
 {

     IElementController ElementController => Element;

     public CustomPickerRenderer(Context context) : base(context)
     {
         AutoPackage = false;

     }


     private AlertDialog _dialog;


     protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
     {
         base.OnElementChanged(e);
         Control.Click += Control_Click;
     }


     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;
         model.Title = "changed";

         var picker = new NumberPicker(Context);

         if (model.Items != null && model.Items.Any())
         {
             // set style here

             picker.MaxValue = model.Items.Count - 1;
             picker.MinValue = 0;

             picker.SetBackgroundColor(Color.White);
             picker.ForegroundTintList = ColorStateList.ValueOf(Color.Black);
             picker.SetDisplayedValues(model.Items.ToArray());
             picker.WrapSelectorWheel = false;
             picker.Value = model.SelectedIndex;

         }


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

         var titleView = new TextView(Context);

         titleView.Text = "hmmmm";
         titleView.SetBackgroundColor(Color.ForestGreen);

         ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);

         var builder = new AlertDialog.Builder(Context);
         builder.SetView(layout);

         builder.SetTitle(model.Title ?? "");

         //builder.SetCustomTitle(titleView);
         builder.SetNegativeButton("Cancelar  ", (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("Confirmar ", (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;
         });

         Control.Text = "Control";

         _dialog = builder.Create();
         _dialog.DismissEvent += (ssender, args) =>
         {
             ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
         };
         _dialog.Show();
     }

    }
 }