根据文本需要多少 space 自动设置 Label HeightRequest
Set Label HeightRequest automatically depending on how much space the text needs
我有一个 Label
里面有一个 StackLayout
里面有一个 Frame
:
<Frame Grid.Row="0" Margin="0, 0, -10, 0" OutlineColor="Transparent" HasShadow="False" Grid.Column="1" BackgroundColor="{StaticResource rightBubbleColor}">
<StackLayout>
<Label
Style="{StaticResource rightBubbleStyle}"
TextColor="{StaticResource rightBubbleFontColor}"
Text="{Binding message}" />
</StackLayout>
</Frame>
这给了我这个结果:
这里的问题是最后一条消息实际上有更多的文本,需要 6 行才能显示整个文本。
如果我为 Label
设置 HeightRequest
的值,它也会更改 Frame
的高度。例如,如果我将值设置为 150,我将得到以下结果:
我需要根据 Text
需要的高度动态设置 HeightRequest
的值。
有谁知道我如何做到这一点(最好是在 xaml 中),或者这甚至是解决我的问题的错误方法?
编辑 1:
从 Label
中删除样式并不能解决问题。它只发生在 UWP 上。请注意,我没有 Windows Phone 来测试它,问题出现在我的本地机器上(Windows 10 桌面)。
编辑 2:
根据Xamarin documentation,高度应该根据内容自动调整大小:
When the app developer sets the ListView.HasUnevenRows property to true, the behavior of the list view still depends on the ListView.RowHeight property. First, if the developer either does not set the ListView.RowHeight property or sets it to -1, list view items are autosized to fit their contents. This is the desired behavior and the intended use case for a ListView.HasUnevenRows value of true, as noted above.
我找到了解决办法。这是问题所在:
<RowDefinition Height="*" />
我将值设置为 Auto
,现在可以使用了。奇怪的是它在 iOS 和 Android 上有不同的行为。
我有一个 Label
里面有一个 StackLayout
里面有一个 Frame
:
<Frame Grid.Row="0" Margin="0, 0, -10, 0" OutlineColor="Transparent" HasShadow="False" Grid.Column="1" BackgroundColor="{StaticResource rightBubbleColor}">
<StackLayout>
<Label
Style="{StaticResource rightBubbleStyle}"
TextColor="{StaticResource rightBubbleFontColor}"
Text="{Binding message}" />
</StackLayout>
</Frame>
这给了我这个结果:
这里的问题是最后一条消息实际上有更多的文本,需要 6 行才能显示整个文本。
如果我为 Label
设置 HeightRequest
的值,它也会更改 Frame
的高度。例如,如果我将值设置为 150,我将得到以下结果:
我需要根据 Text
需要的高度动态设置 HeightRequest
的值。
有谁知道我如何做到这一点(最好是在 xaml 中),或者这甚至是解决我的问题的错误方法?
编辑 1:
从 Label
中删除样式并不能解决问题。它只发生在 UWP 上。请注意,我没有 Windows Phone 来测试它,问题出现在我的本地机器上(Windows 10 桌面)。
编辑 2:
根据Xamarin documentation,高度应该根据内容自动调整大小:
When the app developer sets the ListView.HasUnevenRows property to true, the behavior of the list view still depends on the ListView.RowHeight property. First, if the developer either does not set the ListView.RowHeight property or sets it to -1, list view items are autosized to fit their contents. This is the desired behavior and the intended use case for a ListView.HasUnevenRows value of true, as noted above.
我找到了解决办法。这是问题所在:
<RowDefinition Height="*" />
我将值设置为 Auto
,现在可以使用了。奇怪的是它在 iOS 和 Android 上有不同的行为。