更改 Xamarin Forms Picker 按钮样式
Changing Xamarin Forms Picker button style
我正在尝试在我的 xamarin 表单应用程序中获得一些样式一致性(目前仅针对 android)。我试图让所有按钮看起来都一样。通常,这很容易通过在 App.xaml 文件中添加一个 Style 并将 TargetType 设置为 Button 来完成。问题是我使用的是 Picker(也是具有类似问题的 TimePicker)。单击此选择器时,会显示一个带有取消按钮的弹出窗口。我无法更改此取消按钮的样式(例如更改背景和边框)。
这个虚拟项目的屏幕截图更多地解释了这个问题:
除了尝试App.xaml的解决方案,我还尝试通过调整styles.xml来设置android项目中的样式。这确实也对除选择器对话框中的按钮之外的所有按钮产生了影响。
有人知道如何解决它吗?
格特,
为 Picker 的样式找到了一些东西。这是 Android .
更改取消的颜色或更改它的文本,没有尝试调整字体或其他东西
[assembly: ExportRenderer(typeof(Picker), typeof(MyPickerRenderer))]
namespace Pickerstyle.Droid
{
public class MyPickerRenderer : Xamarin.Forms.Platform.Android.PickerRenderer
{
Typeface fontFace = null;
private IElementController ElementController => Element as IElementController;
private AlertDialog _dialog;
public MyPickerRenderer(Context context) : base(context)
{
AutoPackage = false;
}
[Obsolete("This constructor is obsolete as of version 2.5. Please use PickerRenderer(Context) instead.")]
public MyPickerRenderer()
{
AutoPackage = false;
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (e.NewElement == null || e.OldElement != null || Control == null)
return;
// fontFace = Typeface.CreateFromAsset(this.Context.Assets, "somefont.ttf");
GradientDrawable gd = new GradientDrawable();
gd.SetStroke(0, Android.Graphics.Color.Transparent);
Control.SetBackground(gd);
Control.TextSize = 18f;
Control.SetTypeface(fontFace, TypefaceStyle.Normal);
Control.Click += Control_Click;
}
protected override void Dispose(bool disposing)
{
Control.Click -= Control_Click;
base.Dispose(disposing);
}
private void Control_Click(object sender, EventArgs e)
{
Picker model = Element;
NumberPicker picker = new NumberPicker(Context);
int count = picker.ChildCount;
for (int i = 0; i < count; i++)
{
Android.Views.View v = picker.GetChildAt(i);
if (v.GetType() == typeof(EditText))
{
Java.Lang.Reflect.Field field = picker.Class.GetDeclaredField("mSelectorWheelPaint");
field.Accessible = true;
((Paint)field.Get(picker)).SetTypeface(fontFace);
((EditText)v).SetTypeface(fontFace, TypefaceStyle.Normal);
picker.Invalidate();
}
}
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;
picker.Visibility = ViewStates.Visible;
}
var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
layout.Visibility = ViewStates.Visible;
layout.AddView(picker);
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);
var builder = new AlertDialog.Builder(Context);
builder.SetView(layout);
builder.SetTitle(model.Title ?? "");
builder.SetNegativeButton("Cancel", (s, a) =>
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
Control?.ClearFocus();
_dialog = null;
});
builder.SetPositiveButton("Or this", (s, a) =>
{
ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
if (Element != null)
{
if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
Control.Text = model.Items[Element.SelectedIndex];
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
Control?.ClearFocus();
}
_dialog = null;
});
_dialog = builder.Create();
_dialog.DismissEvent += (ssender, args) =>
{
ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
};
_dialog.Show();
Android.Widget.Button nbutton = _dialog.GetButton((int)Android.Content.DialogButtonType.Positive);
nbutton.SetTypeface(fontFace, TypefaceStyle.Normal);
nbutton.SetTextColor(Android.Graphics.Color.ParseColor("#ff9500"));
nbutton.TextSize = 16f;
LinearLayout layOut = (LinearLayout)nbutton.Parent;
layOut.SetGravity(GravityFlags.CenterHorizontal);
Android.Views.View v1 = layOut.GetChildAt(1);
v1.Visibility = ViewStates.Gone;
int res = Resources.GetIdentifier("alertTitle", "id", "android");
TextView textView = (TextView)_dialog.FindViewById(res);
textView.SetTextColor(Android.Graphics.Color.Green);
textView.SetTypeface(fontFace, TypefaceStyle.Normal);
textView.Gravity = GravityFlags.Center;
}
}
}
找到了一种相当简单的方法来调整选择器弹出窗口上的按钮样式,只需调整 styles.xml。此解决方案仅适用于 android。
在这里找到解决方案:
styles.xml 看起来像这样:
<style name="MainTheme" >
<item name="buttonBarNegativeButtonStyle">@style/ButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/ButtonStyle</item>
<item name="android:buttonStyle">@style/ButtonStyle</item>
</style>
<style name="ButtonStyle" >
<item name="android:textColor">#000000</item>
<item name="android:background">#FF0000</item>
</style>
buttonBarNegativeButtonStyle,改变取消按钮样式。
该解决方案还将调整例如 TimePicker 中的按钮。 (这也有 buttonBarPositiveButtonStyle)
如果你想控制整个picker popup,那么Bas H的方案会更合适
我正在尝试在我的 xamarin 表单应用程序中获得一些样式一致性(目前仅针对 android)。我试图让所有按钮看起来都一样。通常,这很容易通过在 App.xaml 文件中添加一个 Style 并将 TargetType 设置为 Button 来完成。问题是我使用的是 Picker(也是具有类似问题的 TimePicker)。单击此选择器时,会显示一个带有取消按钮的弹出窗口。我无法更改此取消按钮的样式(例如更改背景和边框)。
这个虚拟项目的屏幕截图更多地解释了这个问题:
除了尝试App.xaml的解决方案,我还尝试通过调整styles.xml来设置android项目中的样式。这确实也对除选择器对话框中的按钮之外的所有按钮产生了影响。
有人知道如何解决它吗?
格特, 为 Picker 的样式找到了一些东西。这是 Android .
更改取消的颜色或更改它的文本,没有尝试调整字体或其他东西
[assembly: ExportRenderer(typeof(Picker), typeof(MyPickerRenderer))]
namespace Pickerstyle.Droid
{
public class MyPickerRenderer : Xamarin.Forms.Platform.Android.PickerRenderer
{
Typeface fontFace = null;
private IElementController ElementController => Element as IElementController;
private AlertDialog _dialog;
public MyPickerRenderer(Context context) : base(context)
{
AutoPackage = false;
}
[Obsolete("This constructor is obsolete as of version 2.5. Please use PickerRenderer(Context) instead.")]
public MyPickerRenderer()
{
AutoPackage = false;
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (e.NewElement == null || e.OldElement != null || Control == null)
return;
// fontFace = Typeface.CreateFromAsset(this.Context.Assets, "somefont.ttf");
GradientDrawable gd = new GradientDrawable();
gd.SetStroke(0, Android.Graphics.Color.Transparent);
Control.SetBackground(gd);
Control.TextSize = 18f;
Control.SetTypeface(fontFace, TypefaceStyle.Normal);
Control.Click += Control_Click;
}
protected override void Dispose(bool disposing)
{
Control.Click -= Control_Click;
base.Dispose(disposing);
}
private void Control_Click(object sender, EventArgs e)
{
Picker model = Element;
NumberPicker picker = new NumberPicker(Context);
int count = picker.ChildCount;
for (int i = 0; i < count; i++)
{
Android.Views.View v = picker.GetChildAt(i);
if (v.GetType() == typeof(EditText))
{
Java.Lang.Reflect.Field field = picker.Class.GetDeclaredField("mSelectorWheelPaint");
field.Accessible = true;
((Paint)field.Get(picker)).SetTypeface(fontFace);
((EditText)v).SetTypeface(fontFace, TypefaceStyle.Normal);
picker.Invalidate();
}
}
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;
picker.Visibility = ViewStates.Visible;
}
var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
layout.Visibility = ViewStates.Visible;
layout.AddView(picker);
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);
var builder = new AlertDialog.Builder(Context);
builder.SetView(layout);
builder.SetTitle(model.Title ?? "");
builder.SetNegativeButton("Cancel", (s, a) =>
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
Control?.ClearFocus();
_dialog = null;
});
builder.SetPositiveButton("Or this", (s, a) =>
{
ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
if (Element != null)
{
if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
Control.Text = model.Items[Element.SelectedIndex];
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
Control?.ClearFocus();
}
_dialog = null;
});
_dialog = builder.Create();
_dialog.DismissEvent += (ssender, args) =>
{
ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
};
_dialog.Show();
Android.Widget.Button nbutton = _dialog.GetButton((int)Android.Content.DialogButtonType.Positive);
nbutton.SetTypeface(fontFace, TypefaceStyle.Normal);
nbutton.SetTextColor(Android.Graphics.Color.ParseColor("#ff9500"));
nbutton.TextSize = 16f;
LinearLayout layOut = (LinearLayout)nbutton.Parent;
layOut.SetGravity(GravityFlags.CenterHorizontal);
Android.Views.View v1 = layOut.GetChildAt(1);
v1.Visibility = ViewStates.Gone;
int res = Resources.GetIdentifier("alertTitle", "id", "android");
TextView textView = (TextView)_dialog.FindViewById(res);
textView.SetTextColor(Android.Graphics.Color.Green);
textView.SetTypeface(fontFace, TypefaceStyle.Normal);
textView.Gravity = GravityFlags.Center;
}
}
}
找到了一种相当简单的方法来调整选择器弹出窗口上的按钮样式,只需调整 styles.xml。此解决方案仅适用于 android。
在这里找到解决方案:
styles.xml 看起来像这样:
<style name="MainTheme" >
<item name="buttonBarNegativeButtonStyle">@style/ButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/ButtonStyle</item>
<item name="android:buttonStyle">@style/ButtonStyle</item>
</style>
<style name="ButtonStyle" >
<item name="android:textColor">#000000</item>
<item name="android:background">#FF0000</item>
</style>
buttonBarNegativeButtonStyle,改变取消按钮样式。 该解决方案还将调整例如 TimePicker 中的按钮。 (这也有 buttonBarPositiveButtonStyle)
如果你想控制整个picker popup,那么Bas H的方案会更合适