c# - Java.Lang.NoSuchMethodError : 'no non-static method "Landroid/widget/TextView;. setJustificationMode(I)V"'

标签 c# xaml xamarin.forms

我使用自定义渲染对文本进行排序,但它在某些设备上不起作用并引发此错误:

Java.Lang.NoSuchMethodError: '没有非静态方法“Landroid/widget/TextView;.setJustificationMode(I)V”'

如果有人帮忙,谢谢。

这是我的代码:



[assembly: ExportRenderer(typeof(CustomLabel), typeof(CustomLabelRender))]


namespace customlabel.Droid


{


    public class CustomLabelRender : LabelRenderer


    {
        public CustomLabelRender(Context context) : base(context)
        {

        }


        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.JustificationMode = JustificationMode.InterWord;
            }
        }


    }


}

最佳答案

我尝试使用以下代码,它在我的本地站点中有效:

public class CustomLabelRenderer: LabelRenderer
{
    public CustomLabelRenderer(Context context) : base(context)
    {

    }


    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);

        if (Xamarin.Essentials.DeviceInfo.Version.Major >= 8)
        {
            if (Control != null)
            {
                Control.SetBackgroundColor(Android.Graphics.Color.AliceBlue);
                Control.JustificationMode = JustificationMode.InterWord;
            }
        }
        else
        {
            if (Control != null)
            {
                Control.SetBackgroundColor(Android.Graphics.Color.Red);
                Control.TextAlignment = Android.Views.TextAlignment.Center;
            }
        }
    }
}

Android 9.0设备上的效果:

enter image description here

Android 7.0设备上的效果:

enter image description here

关于c# - Java.Lang.NoSuchMethodError : 'no non-static method "Landroid/widget/TextView;. setJustificationMode(I)V"',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65099781/

相关文章:

c# - 使用 System.IO.Packaging 创建大型 ZIP 文件时出现 OutOfMemoryException

c# - wkhtmltopdf 标题、图像和页码

javascript - 如何将变量从 webview Javascsript 传递到 Xamarin Forms 代码?

c# - 将 TAB 空格插入 TextBox

wpf - 使用属性面板中的 TypeConverter 设置类类型的属性会生成扩展的 XAML,而不是字符串

c# - Xamarin.Forms Shell GoToAsync 在 iOS 中无法按预期工作

c# - 在不知道谁是父级的情况下从父级中删除 View

c# - 从数据读取器填充数据表

c# - WPF C# DataGrid 编辑单元格

c# - Xamarin Forms 错误 => "jarsigner.exe"以代码 1 退出