Xamarin Forms:如何在 Android 中更改选取器控件弹出窗口的背景颜色
Xamarin Forms: How to change the background color of the Picker Controls popup in Android
我不知道如何更改显示在选取器控件上的弹出窗口的背景或按钮。我创建了一个自定义渲染器,它能够通过样式更改许多我出于某种原因无法访问的字段。但是弹出窗口本身的背景细节和底部的按钮仍然难以捉摸。有人有解决办法吗?理想情况下如何在样式或自定义渲染器中执行此操作,因为我已经弄清楚如何将它们插入到我的主题服务中?
到目前为止,这是我在自定义渲染器中的逻辑核心:
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);
你可以看看Renderer Base Classes and Native Controls,其实你这里的Control
相当于Android上的EditText
,所以你换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(在 android 项目中的 Resources/layout
中创建):
<?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
in 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>
我不知道如何更改显示在选取器控件上的弹出窗口的背景或按钮。我创建了一个自定义渲染器,它能够通过样式更改许多我出于某种原因无法访问的字段。但是弹出窗口本身的背景细节和底部的按钮仍然难以捉摸。有人有解决办法吗?理想情况下如何在样式或自定义渲染器中执行此操作,因为我已经弄清楚如何将它们插入到我的主题服务中?
到目前为止,这是我在自定义渲染器中的逻辑核心:
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);
你可以看看Renderer Base Classes and Native Controls,其实你这里的Control
相当于Android上的EditText
,所以你换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(在 android 项目中的 Resources/layout
中创建):
<?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
in 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>