我使用自定义渲染对文本进行排序,但它在某些设备上不起作用并引发此错误:
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设备上的效果:
在Android 7.0设备上的效果:
关于c# - Java.Lang.NoSuchMethodError : 'no non-static method "Landroid/widget/TextView;. setJustificationMode(I)V"',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65099781/