简体   繁体   English

如何设置Xamarin Picker中项目的字体大小?

[英]How to set font size for items in Xamarin Picker?

I try to change the items font size in Xamarin.Picker for my Android app. 我尝试在我的Android应用程序中更改Xamarin.Picker中的项目字体大小。 In my project, I use BindablePicker that inherits from Picker class. 在我的项目中,我使用继承自Picker类的BindablePicker Source here . 来源于

在此输入图像描述

I spent some time to do research and I found that I should create a PickerRenderer class and render the Picker. 我花了一些时间做研究,发现我应该创建一个PickerRenderer类并渲染Picker。

My renderer 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 test purposes I set the backgroundColor for Picker and it works fine. 出于测试目的,我为Picker设置了backgroundColor,它工作正常。 However, in my PickerRenderer class I only have access to Control property which is type of Android.Widget.EditText . 但是,在我的PickerRenderer类中,我只能访问Control属性,它是Android.Widget.EditText类型。

The effect : 效果:

在此输入图像描述

Question

How can I access to Picker items, and set the font size for them? 如何访问Picker项目并为其设置字体大小? Is this possible? 这可能吗?

Here is my repository with an example project. 这是我的一个示例项目的存储库。

https://github.com/k8mil/PickerRendererXamarin https://github.com/k8mil/PickerRendererXamarin

Related links 相关链接

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

Changing the default text color of a Picker control in Xamarin Forms for Windows Phone 8.1 在Xamarin Forms for Windows Phone 8.1中更改Picker控件的默认文本颜色

Font size in Picker control in xamarin forms Xamarin形式的Picker控件中的字体大小

I has been able to resolve this by add the line: 我已经能够通过添加行来解决这个问题:

Control.TextSize = 30;

On OnElementChanged method: On OnElementChanged方法:

public class BindablePickerRenderer : PickerRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);

        if (this.Control != null)
        {
            Control.TextSize = 30;
        }
    }
}

Maybe this can help someone that is looking for change font size of a Bindable picker. 也许这可以帮助那些正在寻找更改Bindable选择器字体大小的人。

After some research I don't think this is possible for a generic picker. 经过一些研究后,我不认为这对于通用选择器是可行的。

You would likely have an easier time just rolling your own picker control in Forms code using a clickable label that pops up a list to select from. 您可以更轻松地使用弹出列表进行选择的可点击标签在Forms代码中滚动自己的选取器控件。

I was able to style a date or time picker using styles in the Android styles.xml file, but since Android does not have a built in generic picker widget, I imagine that Forms is rolling it's own picker list as I can't find the dialog widget to theme that changes your list text size. 我能够使用Android styles.xml文件中的样式设置日期或时间选择器的样式,但由于Android没有内置的通用选择器窗口小部件,我想这些窗体正在滚动它自己的选择器列表,因为我找不到对话框小部件到主题,更改列表文本大小。

For a DatePicker, I can just add the following to the main style element in styles.xml: 对于DatePicker,我可以将以下内容添加到styles.xml中的主样式元素:

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

and then add a new style element in style.xml 然后在style.xml中添加一个新的样式元素

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

The above does change the text size for a DatePicker (no custom renderer required). 以上确实更改了DatePicker的文本大小(不需要自定义渲染器)。 Also the customer picker renderer is a bit of a misnomer... it really is just rendering the edit text field that shows the picked item, and allows opening the picker list on click. 此外,客户选择器渲染器有点用词不当......它实际上只是渲染显示所选项目的编辑文本字段,并允许在单击时打开选择器列表。

I know this is not a solution, but just an indication as to what I found when checking this out and a suggestion that it is likely easiest to not use the Forms Picker type if you want such customization. 我知道这不是一个解决方案,而只是表明我在检查时发现了什么,并建议如果你想要这样的自定义,最简单的方法是不使用Forms Picker类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM