我是Xamarin的新手,我目前正在使用Xamarin Forms PCL进行项目。
有没有办法更改Picker的字体颜色和大小?
<Picker x:Name="pkr_color" Grid.Row="4" Grid.Column="1" HorizontalOptions="FillAndExpand"
BackgroundColor="#ededed" Title="Select Color">
<Picker.Items>
<x:String>Red</x:String>
<x:String>Blue</x:String>
<x:String>Green</x:String>
</Picker.Items>
</Picker>
提前致谢!
答案 0 :(得分:4)
可以使用PCL代码更改选择器的字体大小。
创建MainPage.xaml文件
<Picker x:Name="PickerList" Title="Select Any One" IsVisible="False" SelectedIndexChanged="PickerList_SelectedIndexChanged">
<Picker.Items>
<x:String>Option 1</x:String>
<x:String>Option 2</x:String>
<x:String>Option 3</x:String>
</Picker.Items>
</Picker>
<Label x:Name="PickerLabel" Text="Tap to Select Picker" FontSize="14" HorizontalOptions="Start">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
</Label>
创建MainPage.xaml.cs文件
private void PickerList_SelectedIndexChanged(object sender, EventArgs e)
{
PickerLabel.Text = PickerList.Items[PickerList.SelectedIndex];
// PickerLabel.Text = PickerList.SelectedItem.ToString() ;
}
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
PickerList.Focus();
}
这解决了Android和IOS的问题。
答案 1 :(得分:2)
您需要为每个平台编写自定义渲染器。
using System;
using Project.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer (typeof (Picker), typeof (CustomPickerRenderer))]
namespace Project.iOS
{
public class CustomPickerRenderer : PickerRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged (e);
if (Control != null) {
Control.TextColor = UIKit.UIColor.White;
}
}
}
}
以下是iOS的示例。这会改变文本的颜色,你需要为Android做类似的事情,并且只是添加你的字体大小更改。
答案 2 :(得分:1)
我希望下面的代码有助于获取TextColor
**In Xaml**
<Picker SelectedIndexChanged="OnColorPickerSelected" TextColor="{Binding TextColor}"/>
**In Code Behind**
private void OnColorPickerSelected(object sender, EventArgs e)
{
((ViewModel)BindingContext).Color= pkr_color.Items[pkr_color.SelectedIndex];
ChooseColorPickerTextColor(((ViewModel)BindingContext).Color, pkr_color);
}
**Implement ChooseColorPickerTextColor Method Here**
private void ChooseColorPickerTextColor(string selectedColor, Picker pickerName)
{
Picker colorPickerTextColor = pickerName;
if (selectedColor == "Red")
{
colorPickerTextColor.TextColor = Color.Red;
}
else if (selectedColor == "Yellow")
{
colorPickerTextColor.TextColor = Color.Yellow;
}
else if (selectedColor == "Green")
{
colorPickerTextColor.TextColor = Color.Green;
}
else if (selectedColor == "Blue")
{
colorPickerTextColor.TextColor = Color.Blue;
}
else if (selectedColor == "Maroon")
{
colorPickerTextColor.TextColor = Color.Maroon;
}
else if (selectedColor == "Pink")
{
colorPickerTextColor.TextColor = Color.Pink;
}
else
{
colorPickerTextColor.TextColor = Color.Aqua;
}
}
使用“WidthRequest”我们可以增加选择器的大小
答案 3 :(得分:0)
要更改字体,大小,下划线,文本,textcolor,警告对话框按钮位置,Android原生编号选择器(xamarin表单选取器)中的按钮文本,您可以使用如下自定义渲染处理它:
using System;
using System.Collections.Generic;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Graphics.Drawables;
using Android.Graphics;
[assembly: ExportRenderer(typeof(Picker), typeof(MyPickerRenderer))]
namespace Daddy.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 = 14f;
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("This One", (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("#33b5e5"));
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.Gray);
textView.SetTypeface(fontFace, TypefaceStyle.Normal);
textView.Gravity = GravityFlags.Center;
}
}
}
答案 4 :(得分:-2)
将Label
和Picker
放在同一Grid cell
。不要设置Title
的{{1}}而不是picker
Text
将作为Label
。
Title
现在,当<Label x:Name="PickerLabel" Text="Picker Title" TextColor="Any Color"></Label>
<Picker x:Name="Picker" SelectedIndexChanged="Picker_SelectedIndexChanged" TextColor="Any Color" />
Text
Invisible
来自Item
时,生成标签selected
的{{1}}。
Picker
答案 5 :(得分:-3)
一个选项可以,使用Scale。例如,在xaml中:
<Picker Scale="0.5">
<Picker.Items>
<x:String>Red</x:String>
<x:String>Blue</x:String>
<x:String>Green</x:String>
</Picker.Items>
</Picker>