Xamarin形成XAML标签旋转
我对Xamarin有问题。表格和标签。 我正在尝试在网格列上设置标签 这里的第一幅图显示了预期的结果,它是在Android上用AXML编写的 这里的第二个图像是用Xamarin.Forms中的XAML编写的 XAML文件中的代码如下所示:Xamarin形成XAML标签旋转,xamarin,rotation,label,xamarin.forms,Xamarin,Rotation,Label,Xamarin.forms,我对Xamarin有问题。表格和标签。 我正在尝试在网格列上设置标签 这里的第一幅图显示了预期的结果,它是在Android上用AXML编写的 这里的第二个图像是用Xamarin.Forms中的XAML编写的 XAML文件中的代码如下所示: <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> <Grid.ColumnDefinitions>
<Grid
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400*"/>
<ColumnDefinition Width="75*"/>
</Grid.ColumnDefinitions>
<WebView Source="{Binding ContentSource}" />
<!--<ProgressBar IsVisible="{Binding IsLoading}"
Progress="{Binding Progress}"/>-->
<Grid Grid.Column="1"
BackgroundColor="#EE7F00"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<Label
Text="{Binding DocumentIndex}"
LineBreakMode="NoWrap"
HorizontalOptions="Center"
Rotation="-90"
VerticalOptions="Center" />
</Grid>
</Grid>
如何将标签的高度或宽度扩展到与文本长度相等
到目前为止,非常感谢删除用于标签的网格容器,然后放置一个长方体视图,并为长方体视图和标签设置相同的网格行和列。像这样
<Grid
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80*"/>
<ColumnDefinition Width="20*"/>
</Grid.ColumnDefinitions>
<WebView Grid.Row="0" Grid.Column="0" Source="{Binding ContentSource}" />
<!--<ProgressBar IsVisible="{Binding IsLoading}"
Progress="{Binding Progress}"/>-->
<BoxView Grid.Row="0" Grid.Column="1" BackgroundColor="#EE7F00" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding DocumentIndex}"
LineBreakMode="NoWrap"
HorizontalOptions="Center"
Rotation="-90"
VerticalOptions="Center" />
</Grid>
我希望这将解决旋转后的标签长度问题。我使用自定义渲染器解决了这个问题。在表单项目中:
public class RotatedText : View
{
public static BindableProperty TitleValueProperty = BindableProperty.Create(nameof(TitleValue), typeof(string), typeof(string), null, BindingMode.TwoWay, null,
(bindable, oldValue, newValue) =>
{
});
public string TitleValue
{
get => (string)GetValue(TitleValueProperty);
set => SetValue(TitleValueProperty, value);
}
}
在您的Android项目中:
[assembly: ExportRenderer(typeof(RotatedText), typeof(RotatedTextRenderer))]
namespace MyNamespace
{
public class RotatedTextRenderer : ViewRenderer
{
private Context _context;
public RotatedTextRenderer(Context c) : base(c)
{
_context = c;
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
if (e.NewElement is RotatedText)
{
string title = ((RotatedText)e.NewElement).TitleValue;
SetNativeControl(new RotatedTextView(_context, title));
}
}
}
public class RotatedTextView : Android.Views.View
{
private int DEFAULT_TEXT_SIZE = 30;
private string _text;
private TextPaint _textPaint;
public RotatedTextView(Context c, string title) : base(c)
{
_text = title;
initLabelView();
}
private void initLabelView()
{
this._textPaint = new TextPaint();
this._textPaint.AntiAlias = true;
this._textPaint.TextAlign = Paint.Align.Center;
this._textPaint.TextSize = DEFAULT_TEXT_SIZE;
this._textPaint.Color = new Android.Graphics.Color(0, 0, 0);
}
public override void Draw(Canvas canvas)
{
base.Draw(canvas);
if (!string.IsNullOrEmpty(this._text))
{
float x = (Width / 2) - DEFAULT_TEXT_SIZE/3;
float y = (Height / 2);
canvas.Rotate(90);
canvas.DrawText(this._text, y, -x, this._textPaint);
}
}
}
}
[程序集:导出呈现程序(typeof(RotatedText)、typeof(rotatedextender))]
名称空间MyNamespace
{
公共类RotatedExtrender:ViewRenderer
{
私人语境(private Context)(私人语境);;
公共轮换外部推荐人(上下文c):基础(c)
{
_上下文=c;
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
如果(例如,新元素为旋转文本)
{
字符串标题=((RotatedText)e.NewElement).TitleValue;
SetNativeControl(新的RotatedTextView(_上下文,标题));
}
}
}
公共类RotatedTextView:Android.Views.View
{
私有int默认值\文本\大小=30;
私有字符串_文本;
私人TextPaint_TextPaint;
公共旋转文本视图(上下文c,字符串标题):基(c)
{
_文本=标题;
initLabelView();
}
私有void initLabelView()
{
这是._textPaint=new textPaint();
这是。_textPaint.antialas=true;
这是._textPaint.TextAlign=Paint.Align.Center;
这是.\u textPaint.TextSize=默认的\u TEXT\u大小;
这个._textPaint.Color=newandroid.Graphics.Color(0,0,0);
}
公共覆盖无效绘制(画布)
{
绘制(画布);
如果(!string.IsNullOrEmpty(此._文本))
{
浮点x=(宽度/2)-默认值\文本\大小/3;
浮动y=(高度/2);
画布。旋转(90);
canvas.DrawText(this.\u text,y,-x,this.\u textPaint);
}
}
}
}
然后只需将
标题值
设置在使用旋转文本
的位置。这有点难看,但我找不到更好的方法。尝试使用CenterAndExpand而不是标签上的“中心”。这对我不起作用。我要面对同样的问题,即使在旋转后,标签也会以父宽度呈现。增加标签字体/文本会增加父级宽度,需要在Xamarin Bugzilla()中将其作为错误进行归档,我也遇到过类似的问题。栅格内的标签旋转+或-90度(在默认z轴上),并约束在栅格布局内的窄列内。标签文本将被截断为列的宽度。我已经尝试了上述建议的解决方法,但没有任何区别。我已经在Android和iOS平台上试用过了。这是一个已知的问题。上面1月5日的评论中提到了Xamarin bugzilla。旋转-90而不是90对我很有帮助