首页 > 解决方案 > Xamarin Forms:如何在 Android 中更改 Picker Controls 弹出窗口的背景颜色

问题描述

我无法弄清楚如何更改背景或出现在 Picker 控件上的弹出窗口上的按钮。我创建了一个自定义渲染器,它能够通过样式更改由于某种原因我无法访问的许多字段。但是弹出窗口本身的背景和底部按钮的细节仍然难以捉摸。有人有解决方案吗?理想情况下,如何在样式或自定义渲染器中做到这一点,因为我已经弄清楚如何将它们插入我的主题服务?

到目前为止,这是我在自定义渲染器中的逻辑核心:

var xamarinSelectedColor = (Color)Application.Current.Resources["SelectedColor"];
var androidSelectedColor = xamarinSelectedColor.ToAndroid();

Control.SetHighlightColor(androidSelectedColor);
Control.BackgroundTintList = ColorStateList.ValueOf(androidSelectedColor);

var xamarinTextOnBackgroundColor = (Color)Application.Current.Resources["TextColorOverBackground"];
var androidTextOnBackgroundColor = xamarinTextOnBackgroundColor.ToAndroid();

Control.SetTextColor(androidTextOnBackgroundColor);

标签: c#xamarinxamarin.android

解决方案


你可以看看Renderer Base Classes 和 Native Controls,其实你Control这里和EditTextAndroid上是等价的,所以直接改Control是不行的。

您需要Dialog在自定义渲染器中定义自定义。

例如:

public class AndroidPickerRenderer : PickerRenderer
{
    private Context context;
    AlertDialog listDialog;
    string[] items;
    Color xamarinSelectedColor;
    Color xamarinTextOnBackgroundColor;
    public AndroidPickerRenderer(Context context) : base(context)
    {
        this.context = context;
    }
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);
        xamarinSelectedColor = (Color)Application.Current.Resources["SelectedColor"];
        xamarinTextOnBackgroundColor = (Color)Application.Current.Resources["TextColorOverBackground"];
         
        if (Control != null)
        {
            Control.Click += Control_Click;
        }
    }

    private void Control_Click(object sender, EventArgs e)
    {
        Picker model = Element;
        items = model.Items.ToArray();
        AlertDialog.Builder builder =
       new AlertDialog.Builder(context);
        builder.SetTitle(model.Title ?? "");
        builder.SetNegativeButton("Cancel", (s, a) =>
        {
            Control?.ClearFocus();
            builder = null;
        });
        Android.Views.View view = LayoutInflater.From(context).Inflate(Resource.Layout.listview, null);
        Android.Widget.ListView listView = view.FindViewById<Android.Widget.ListView>(Resource.Id.listView1);

        MyAdapter myAdapter = new MyAdapter(items, Element.SelectedIndex);
        listView.Adapter = myAdapter;
        listView.ItemClick += ListView_ItemClick;
        builder.SetView(view);
        listDialog = builder.Create();
        listDialog.Window.DecorView.SetBackgroundColor(xamarinSelectedColor.ToAndroid()); // set the dialog background color
        listDialog.Show();
        Android.Widget.Button button = listDialog.GetButton((int)DialogButtonType.Negative);
        button.SetTextColor(xamarinTextOnBackgroundColor.ToAndroid()); // set the button bottom color
    }

    private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        Control.Text = items[e.Position];
        Element.SelectedIndex = e.Position;
        listDialog.Dismiss();
        listDialog = null;
    }

    class MyAdapter : BaseAdapter
    {
        private string[] items;
        private int selectedIndex;

        public MyAdapter(string[] items)
        {
            this.items = items;
        }

        public MyAdapter(string[] items, int selectedIndex) : this(items)
        {
            this.selectedIndex = selectedIndex;
        }

        public override int Count => items.Length;

        public override Java.Lang.Object GetItem(int position)
        {
            return items[position];
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override Android.Views.View GetView(int position, Android.Views.View convertView, ViewGroup parent)
        {
            if (convertView == null)
            {
                convertView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.listview_item, null);
            }
            TextView textView = convertView.FindViewById<TextView>(Resource.Id.textView1);
            textView.Text = items[position];
            return convertView;
        }
    }
}

listview.xml(在你Resources/layout的 android 项目中创建):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
       android:id="@+id/listView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>


</LinearLayout>

listview_item.xml(在你Resources/layout的android项目中创建,你可以自己定义样式):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <TextView
      android:id="@+id/textView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
</LinearLayout>

推荐阅读