【发布时间】:2021-05-15 14:20:45
【问题描述】:
Xamarin Forms Android Autosize Label TextCompat pre android 8 doesn't autosize text
很遗憾,我没有足够高的代表来评论任何人的帖子。
我正在尝试一些事情,发现链接的帖子在尝试其他帖子后让我非常接近解决方案。我还尝试在应用程序中自动调整文本大小,但在 MVVM Master Detail 项目中。如果我直接在 Droid 渲染器中输入值,它会按预期工作,但是当我有所需的所有大小的字体时,这会破坏目的。
- 我已经确定我的返回类型是正确的。
- 后面的代码在获取值之前初始化。
- 这些字段是公开的。
- 插入数值而不是可绑定属性没有其他问题。
我没有收到来自视图的任何值。我假设该视图尚未创建,但后面的代码已初始化。我很确定我所做的一切大部分都是正确的,但我主要处理股票 Xamarin,所以扩展功能对我来说仍然很新。感谢所有帮助。
自定义控件(编辑:将默认值从 default(int) 更改为整数值以消除异常)
/// <summary>Auto scale label font size class.</summary>
public class AutoSizeLabel : Label
{
/// <summary>Minimum font size property.</summary>
public static readonly BindableProperty MinimumFontSizeProperty = BindableProperty.Create(
propertyName: nameof(MinimumFontSize),
returnType: typeof(int),
declaringType: typeof(AutoSizeLabel),
defaultValue: 17);
/// <summary>Maximum font size property.</summary>
public static readonly BindableProperty MaximumFontSizeProperty = BindableProperty.Create(
propertyName: nameof(MaximumFontSize),
returnType: typeof(int),
declaringType: typeof(AutoSizeLabel),
defaultValue: 24);
/// <summary>Gets or sets minimum font size.</summary>
public int MinimumFontSize
{
get
{
return (int)this.GetValue(MinimumFontSizeProperty);
}
set
{
this.SetValue(MinimumFontSizeProperty, value);
}
}
/// <summary>Gets or sets maximum font size.</summary>
public int MaximumFontSize
{
get
{
return (int)this.GetValue(MaximumFontSizeProperty);
}
set
{
this.SetValue(MaximumFontSizeProperty, value);
}
}
}
机器人渲染器
public class AutoSizeLabelRenderer : LabelRenderer
{
protected override bool ManageNativeControlLifetime => false;
protected override void Dispose(bool disposing)
{
Control.RemoveFromParent();
base.Dispose(disposing);
}
private AutoSizeLabel bindingValue = new AutoSizeLabel();
private AppCompatTextView appCompatTextView;
public AutoSizeLabelRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (e.NewElement == null || !(e.NewElement is AutoSizeLabel autoLabel) || Control == null) { return; }
//v8 and above supported natively, no need for the extra stuff below.
if (DeviceInfo.Version.Major >= 8)
{
Control?.SetAutoSizeTextTypeUniformWithConfiguration(bindingValue.MinimumFontSize, bindingValue.MaximumFontSize, 2, (int)ComplexUnitType.Sp);
return;
}
appCompatTextView = new AppCompatTextView(Context);
appCompatTextView.SetTextColor(Element.TextColor.ToAndroid());
appCompatTextView.SetMaxLines(1);
appCompatTextView.SetBindingContext(autoLabel.BindingContext);SetNativeControl(appCompatTextView);
TextViewCompat.SetAutoSizeTextTypeUniformWithConfiguration(Control, bindingValue.MinimumFontSize, bindingValue.MaximumFontSize, 2, (int)ComplexUnitType.Sp);
}
}
XAML 调用
<renderer:AutoSizeLabel MinimumFontSize="17"
MaximumFontSize="24"
Style="{StaticResource SomeStyle}"
Text="{Binding SomeText}">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding SomeCommand}"></TapGestureRecognizer>
</Label.GestureRecognizers>
</renderer:AutoSizeLabel>
【问题讨论】:
-
现在的错误是什么?你能否提供更多关于你想要得到什么以及你现在做了什么的截图?您的设备是什么版本?
AutoSize属性有效吗? -
@WendyZang-MSFT Nexus One android 9.0。一个示例是视图中的单个标签。我正在将应用程序翻译成 6 种不同的语言,其中有多种语言可以翻译成比英语更长的字符串。如果您将自定义渲染器中的最小值和最大值设置为实际数字,则自动调整大小可以完美运行。我一直在尝试的大部分工作都是在各处放置断点以追踪返回值为 0 的原因。
Java.Lang.IllegalArgumentException Message=Minimum auto-size text size (0.0px) is less or equal to (0px)
标签: xamarin.forms xamarin.android