c# - 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);
解决方案
你可以看看Renderer Base Classes 和 Native Controls,其实你Control
这里和EditText
Android上是等价的,所以直接改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>
推荐阅读
- python - 根据列表中出现的字符串对数据框行进行分类
- java - 如何打印最长的三个字符串?
- r - Weighted mean of a group, where weight is from another group
- android - React Native:来自 uri 的图像未在 Android 上使用字符串插值来呈现以移交令牌
- java - 流分组列表
- >
- sharepoint - 如何为 Sharepoint2019 的 Windows 身份验证创建 Custom.aspx
- c# - 从sql查询中输出删除的变量在c#中不起作用
- elasticsearch - 在elasticsearch中通过部分匹配搜索字符串数组
- arrays - 在c中创建动态结构数组
- arrays - Swift 固定大小的数组在每次追加后都会改变大小