How to set font size for items in Xamarin Picker?

I am trying to change the font size of elements in Xamarin.Picker for my Android application. In my project, I use a BindablePicker that inherits from the Picker class. The source is here .

enter image description here

I spent some time researching, and I found that I had to create the PickerRenderer class and display the Picker.

My rendering class:

 public class BindablePickerRenderer : PickerRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Picker> e) { base.OnElementChanged(e); var picker = e.NewElement; BindablePicker bp = (BindablePicker)this.Element; if (this.Control != null) { var pickerStyle = new Style(typeof(BindablePicker)) { Setters = { new Setter { Property = VisualElement.BackgroundColorProperty, Value = Color.Red } } }; picker.Style = pickerStyle; } } } 

For testing purposes, I install backgroundColor for Picker and it works fine. However, in my PickerRenderer class PickerRenderer I only have access to the Control property, which is a type of Android.Widget.EditText .

Effect:

enter image description here

Question

How can I access Picker elements and set the font size for them? Is it possible?

Here is my repository with an example project.

https://github.com/k8mil/PickerRendererXamarin

Related Links

https://developer.xamarin.com/api/type/Xamarin.Forms.Picker/

Change the default text color of a Picker control in Xamarin formats for Windows Phone 8.1

Font size in a Picker control in xamarin formats

+5
source share
2 answers

After some research, I don't think this is possible for a universal collector.

You will most likely have a simpler time by simply flipping your own selection control in the Forms code using an interactive label that displays a list of choices.

I managed to style the date or time using styles in the styles.xml file of Android, but since Android does not have a built-in widget widget, I suppose Forms rolls its own list of pickers, t find a dialog widget for a theme that resizes text in the list.

For DatePicker, I can simply add the following to the main style element in styles.xml:

 <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item> 

and then add a new style in style.xml

 <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog"> <item name="android:textSize">60sp</item> </style> 

The above resizes the text for a DatePicker (no custom renderer is required). In addition, the rendering for client selection is a bit wrong ... it's actually just a visual editing of a text field that displays the selected item and allows you to open the selection list by click.

I know that this is not a solution, but simply an indication of what I found when checking this, and the assumption that, most likely, it is easiest to use the Picks Picker type if you want such a setting.

+1
source

I was able to resolve this by adding the line:

 Control.TextSize = 30; 

In OnElementChanged mode:

 public class BindablePickerRenderer : PickerRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Picker> e) { base.OnElementChanged(e); if (this.Control != null) { Control.TextSize = 30; } } } 

Perhaps this can help someone looking for the font size to change the Bindable element.

+2
source

Source: https://habr.com/ru/post/1260638/


All Articles