我制作了一个带有框架和标签的网格。但是网格的高度太大。 我不知道标签中会有多少个单词。
那么有什么方法可以获取框架或标签的高度?
并且网格始终无法适应框架。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="6.6*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackLayout Grid.Column="0" VerticalOptions="CenterAndExpand">
<Image Source="lf.png" />
</StackLayout>
<RelativeLayout Grid.Column="1" VerticalOptions="CenterAndExpand">
<Frame BackgroundColor="#f0f8fa" x:Name="box1" Padding="9,9,20,9"
HasShadow="false">
<Label Text="慢性咽炎怎么办?" TextColor="#111111" FontSize="16" />
</Frame>
<Image Source="chat_left.png" WidthRequest="6"
HeightRequest="7.5" RelativeLayout.XConstraint="{ConstraintExpression
Type=RelativeToView,ElementName=box1,Property=X,Factor=0,Constant=-6}"
RelativeLayout.YConstraint="{ConstraintExpression
Type=RelativeToView,ElementName=box1,Property=Height,
Factor=0.5,Constant=0}" />
</RelativeLayout>
<StackLayout Grid.Column="2">
</StackLayout>
</Grid>
答案 0 :(得分:0)
如果只想让网格适合标签的大小,则只需将行的高度或列的宽度设置为自动 strong>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label x:Name="label1" BackgroundColor="Red" Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" Grid.Row="0" SizeChanged="Label1_SizeChanged"/>
<Label x:Name="label2" BackgroundColor="Yellow" Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbBBBBBBBBBBBBBBBBBBBbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" Grid.Row="1" SizeChanged="Label2_SizeChanged" />
</Grid>
如果您想获取标签的高度,则可以通过 SizeChanged
方法获取后面的代码中
private void Label1_SizeChanged(object sender, EventArgs e)
{
Label label = (Label)sender;
DisplayAlert("Title", "the height of label1 is " + label.Height, "cancel");
}
private void Label2_SizeChanged(object sender, EventArgs e)
{
Label label = (Label)sender;
DisplayAlert("Title", "the height of label2 is " + label.Height, "cancel");
}