简体   繁体   English

Xamarin.Forms Picker 长文本溢出

[英]Xamarin.Forms Picker long text overflow

In Xamarin.Forms Picker if there's a item with very large text it is overflowing in Android.在 Xamarin.Forms Picker 中,如果有一个文本非常大的项目,它就会在 Android 中溢出。 iOS is able to truncate at the end but Android is not able to. iOS 能够在最后截断,但 Android 不能。

<Picker x:Name="picker" Title="Select a monkey" TitleColor="Red">
    <Picker.ItemsSource>
        <x:Array Type="{x:Type x:String}">
            <x:String>Baboon</x:String>
            <x:String>Capuchin Capuchin Capuchin Capuchin Capuchin</x:String>
            <x:String>Blue Monkey</x:String>
            <x:String>Squirrel Monkey</x:String>
            <x:String>Golden Lion Tamarin</x:String>
            <x:String>Howler Monkey</x:String>
            <x:String>Japanese Macaque</x:String>
        </x:Array>
    </Picker.ItemsSource>
</Picker>

溢出图像

See Image for reference.请参阅图像以供参考。

I Guess if there's even a way to left align the items instead of center align, that would still be enough.我想如果有一种方法可以让项目左对齐而不是居中对齐,那也足够了。 But if there's a way to add ellipsis, that'll be the best.但是,如果有办法添加省略号,那将是最好的。 I've looked through the custom renderer and was not able to find anything useful.我查看了自定义渲染器,但找不到任何有用的东西。

As an alternative solution as well that you can change the alignment of the Picker Item(Default is Center), which can make it left.作为替代解决方案,您也可以更改 Picker Item 的对齐方式(默认为居中),这可以使它离开。

Customizing the Text and Style in, Custom Rendering in Android & IOS look at the example here .自定义文本和样式,在 Android 和 IOS 中自定义呈现请看这里的例子。

Also, I found a bug related to the mentioned problem, which could not find any answer.另外,我发现了一个与上述问题相关的错误,找不到任何答案。 I hope the above solution will workaround.我希望上述解决方案能够解决问题。

Create CustomPickerRenderer and inherits with Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer.创建 CustomPickerRenderer 并继承 Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer。 I didn't try this but this should create new picker dialogue without upgrading xamarin forms version.我没有尝试这个,但这应该在不升级 xamarin 表单版本的情况下创建新的选择器对话。

public class CustomPickerRenderer : Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
{
    public CustomPickerRenderer(Context context) : base(context)
    {

    }

    protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
    }
}

I tested the code and I found that the long text will not overflow on my side.我测试了代码,发现长文本不会在我这边溢出。 I tested on the different devices by using different Android versions.我使用不同的 Android 版本在不同的设备上进行了测试。

Here is the view of the code on my side:这是我这边的代码视图:

在此处输入图像描述

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

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