Xamarin Forms Label/StackLayout 旋转问题
Xamarin Forms Label/StackLayout Rotation Issue
我正在尝试旋转 xamarin 表单中的一些标签,但存在一些问题。如果我旋转单个标签,文本将 trim 并且只有一些字母可见,我将每个标签放在一个 stacklayout 中,然后将 stacklayout 本身旋转 -90 度,如下面的代码
<StackLayout Spacing="0"
Rotation="-90"
VerticalOptions="Start"
HorizontalOptions="End">
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
</StackLayout>
标签正确旋转,没有文字 trimmed。现在的问题是堆栈布局的 VerticalOptions
或 HorizontaOptions
无法正常工作。当我将 VerticalOption
设置为 "Start" 时,它会显示不在顶部的所有内容,但会显示距离顶部 25% 的所有内容。当我将 HorizontalOptions
设置为 "Start" 或 "End" 时,HorizontalOptions
出现另一个问题,如下图所示:
任何人都可以帮助解决这个问题,或者是否有更好的方法来解决这个问题?提前致谢
原因:
当你设置 Rotation
时,它会围绕 itself.So 的中心旋转,顶部会有一些 'space'屏幕 .
解决方法:
您可以设置 StackLayout
的边距。
<StackLayout Spacing="0"
Rotation="-90"
Margin="0,-130,0,0"
VerticalOptions="Start"
HorizontalOptions="Center">
...
</StackLayout>
结果如下图。
我同意 Lucas 的回答,使用边距是一个有效的解决方案。
也许你应该调查一下 RelativeLayout? RelativeLayout 可用于相对于整体布局或其他视图在屏幕上定位视图。
注意:由于定义约束的方式,可以在 C# 中创建比使用 XAML 指定的更复杂的布局。
一些有用的链接
- RelativeLayout 文档:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/relative-layout
- 示例应用程序:https://developer.xamarin.com/samples/xamarin-forms/UserInterface/Layout/
- 复杂示例应用程序:https://developer.xamarin.com/samples/xamarin-forms/UserInterface/BusinessTumble/
这是我使用 XAML.
拼凑的示例
<RelativeLayout>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=-30}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=25}"
BackgroundColor="Green"
Rotation="-90"
Text="Hello World"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=-10}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=25}"
BackgroundColor="Blue"
Rotation="-90"
Text="Hello World"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=10}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=25}"
BackgroundColor="Red"
Rotation="-90"
Text="Hello World"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=55}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=18}"
BackgroundColor="Green"
Rotation="-90"
Text="123456"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=75}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=120}"
BackgroundColor="Green"
Rotation="-180"
Text="ABC DEF GHI"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=120}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Green"
Rotation="45"
Text="JKL MNO PQR"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=320}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Green"
Rotation="-270"
Text="Aa Bb Cc Dd"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=0}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Blue"
Rotation="-90"
Text="Aa Bb Cc Dd"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=0}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Green"
Rotation="-70"
Text="Aa Bb Cc Dd"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=0}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Blue"
Rotation="-50"
Text="Aa Bb Cc Dd"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=0}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Green"
Rotation="-30"
Text="Aa Bb Cc Dd"
TextColor="White"
/>
</RelativeLayout>
我正在尝试旋转 xamarin 表单中的一些标签,但存在一些问题。如果我旋转单个标签,文本将 trim 并且只有一些字母可见,我将每个标签放在一个 stacklayout 中,然后将 stacklayout 本身旋转 -90 度,如下面的代码
<StackLayout Spacing="0"
Rotation="-90"
VerticalOptions="Start"
HorizontalOptions="End">
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
<Label Text="ABCDE" TextColor="Black"/>
</StackLayout>
标签正确旋转,没有文字 trimmed。现在的问题是堆栈布局的 VerticalOptions
或 HorizontaOptions
无法正常工作。当我将 VerticalOption
设置为 "Start" 时,它会显示不在顶部的所有内容,但会显示距离顶部 25% 的所有内容。当我将 HorizontalOptions
设置为 "Start" 或 "End" 时,HorizontalOptions
出现另一个问题,如下图所示:
任何人都可以帮助解决这个问题,或者是否有更好的方法来解决这个问题?提前致谢
原因:
当你设置 Rotation
时,它会围绕 itself.So 的中心旋转,顶部会有一些 'space'屏幕 .
解决方法:
您可以设置 StackLayout
的边距。
<StackLayout Spacing="0"
Rotation="-90"
Margin="0,-130,0,0"
VerticalOptions="Start"
HorizontalOptions="Center">
...
</StackLayout>
结果如下图。
我同意 Lucas 的回答,使用边距是一个有效的解决方案。
也许你应该调查一下 RelativeLayout? RelativeLayout 可用于相对于整体布局或其他视图在屏幕上定位视图。
注意:由于定义约束的方式,可以在 C# 中创建比使用 XAML 指定的更复杂的布局。
一些有用的链接
- RelativeLayout 文档: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/relative-layout
- 示例应用程序:https://developer.xamarin.com/samples/xamarin-forms/UserInterface/Layout/
- 复杂示例应用程序:https://developer.xamarin.com/samples/xamarin-forms/UserInterface/BusinessTumble/
这是我使用 XAML.
拼凑的示例
<RelativeLayout>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=-30}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=25}"
BackgroundColor="Green"
Rotation="-90"
Text="Hello World"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=-10}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=25}"
BackgroundColor="Blue"
Rotation="-90"
Text="Hello World"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=10}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=25}"
BackgroundColor="Red"
Rotation="-90"
Text="Hello World"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=55}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=18}"
BackgroundColor="Green"
Rotation="-90"
Text="123456"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=75}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=120}"
BackgroundColor="Green"
Rotation="-180"
Text="ABC DEF GHI"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=120}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Green"
Rotation="45"
Text="JKL MNO PQR"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=320}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Green"
Rotation="-270"
Text="Aa Bb Cc Dd"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=0}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Blue"
Rotation="-90"
Text="Aa Bb Cc Dd"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=0}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Green"
Rotation="-70"
Text="Aa Bb Cc Dd"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=0}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Blue"
Rotation="-50"
Text="Aa Bb Cc Dd"
TextColor="White"
/>
<Label
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,Property=X, Factor=0, Constant=0}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,Property=Y, Factor=0, Constant=200}"
BackgroundColor="Green"
Rotation="-30"
Text="Aa Bb Cc Dd"
TextColor="White"
/>
</RelativeLayout>