【发布时间】:2016-10-11 21:02:20
【问题描述】:
刚刚玩了Xamarin.Forms,我想创建一个带有旋转标签的ListView。我设法让标签旋转,但它们相互重叠。我猜 ListView Items Height 不适应旋转。
这是我的代码
<ListView Grid.Column="0" x:Name="ListView" BackgroundColor="Black">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView Rotation="-90" VerticalOptions="FillAndExpand" HeightRequest="100">
<Label BackgroundColor="#41b5e8" Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center"/>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我尝试绑定到 Label Width,这似乎停止了重叠,但似乎没有考虑标签的边距。
<ContentView Rotation="-90" VerticalOptions="FillAndExpand" HeightRequest="{Binding Source={x:Reference Label}, Path=Width}" >
<Label x:Name="Label" BackgroundColor="#41b5e8" Text="{Binding}" HorizontalOptions="Center" VerticalOptions="Center" Margin="12"/>
</ContentView>
更新 1: 感谢 Adam Pedley,我现在可以正常进行轮换。现在我对标签的文本位置有疑问。我希望它在水平和垂直方向都居中,但是按照以下操作,我的文字会消失。
<Grid x:Name="Layout" Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" x:Name="ListView" BackgroundColor="Green" SeparatorVisibility="None" HorizontalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView Rotation="-90" BackgroundColor="#41b5e8" VerticalOptions="FillAndExpand" HeightRequest="{Binding Source={x:Reference Label}, Path=Width}">
<Label x:Name="Label" BackgroundColor="Red" Text="{Binding}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" />
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Grid Grid.Column="1" HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ContentView x:Name="Button1" HorizontalOptions="FillAndExpand" BindingContext="{Binding WeekDay}"/>
<Button Text="DAY 2" Grid.Column="1" HorizontalOptions="FillAndExpand"/>
<Button Text="DAY 3" Grid.Column="2" HorizontalOptions="FillAndExpand"/>
</Grid>
</Grid>
</Grid>
这样显示
如果我在Layout Grid 中将列定义更改为<ColumnDefinition Width="4*"/>,我明白了。
【问题讨论】:
-
你应该尝试用填充替换边距。