网格内的按钮/标签高度不正确(Xamarin)

cx6n0qe3  于 2023-10-14  发布在  其他
关注(0)|答案(1)|浏览(163)

我在一个网格中有两个按钮,其中一个包含一个较长的文本,不适合一行。带有长文本的按钮似乎获得了不正确的高度,并且某些文本不可见。从我所看到的,高度是一个它会需要如果它是使用屏幕的全宽(与网格外的按钮比较)。标签似乎以类似的方式行事。
代码如下:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. x:Class="App1.MainPage">
  5. <StackLayout>
  6. <Button Text="1 line" />
  7. <Button Text="Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8 Word9 Word10 Word11 Word12 Word13 Word14 Word15 Word16 Word17 Word18 Word19 Word20" />
  8. <Grid BackgroundColor="Cyan">
  9. <Grid.RowDefinitions>
  10. <RowDefinition></RowDefinition>
  11. <RowDefinition></RowDefinition>
  12. </Grid.RowDefinitions>
  13. <Grid.ColumnDefinitions>
  14. <ColumnDefinition Width="*" />
  15. <ColumnDefinition Width="*" />
  16. </Grid.ColumnDefinitions>
  17. <Button Grid.Row="0" Grid.Column="0" Text="1 line" />
  18. <Button Grid.Row="0" Grid.Column="1" Text="Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8 Word9 Word10 Word11 Word12 Word13 Word14 Word15 Word16 Word17 Word18 Word19 Word20" />
  19. <Label Grid.Row="1" Grid.Column="0" BackgroundColor="Green" Text="1 line" />
  20. <Label Grid.Row="1" Grid.Column="1" BackgroundColor="Magenta" Text="Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8 Word9 Word10 Word11 Word12 Word13 Word14 Word15 Word16 Word17 Word18 Word19 Word20" />
  21. </Grid>
  22. </StackLayout>
  23. </ContentPage>

结果是这样的:

我是否在网格定义中遗漏了一些东西,以使其中的元素得到正确的测量?

l2osamch

l2osamch1#

尝试在StackLayout中添加Button

  1. <StackLayout>
  2. <Button Text="1 line" />
  3. <Button Text="Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8 Word9 Word10 Word11 Word12 Word13 Word14 Word15 Word16 Word17 Word18 Word19 Word20" />
  4. <Grid x:Name="grid" BackgroundColor="Cyan">
  5. <Grid.RowDefinitions>
  6. <RowDefinition Height="Auto"/>
  7. <RowDefinition/>
  8. </Grid.RowDefinitions>
  9. <Grid.ColumnDefinitions>
  10. <ColumnDefinition Width="*" />
  11. <ColumnDefinition Width="*" />
  12. </Grid.ColumnDefinitions>
  13. <StackLayout Grid.Row="0" Grid.Column="0">
  14. <Button x:Name="btn1" Text="1 line" VerticalOptions="Start"/>
  15. </StackLayout>
  16. <StackLayout Grid.Row="0" Grid.Column="1">
  17. <Button x:Name="btn2" Grid.Row="0" Grid.Column="1" Text="Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8 Word9 Word10 Word11 Word12 Word13 Word14 Word15 Word16 Word17 Word18 Word19 Word20 Word21 Word22 Word23 Word24" SizeChanged="btn2_SizeChanged" VerticalOptions="Start"/>
  18. </StackLayout>
  19. <Label Grid.Row="1" Grid.Column="0" BackgroundColor="Green" Text="1 line" />
  20. <Label Grid.Row="1" Grid.Column="1" BackgroundColor="Magenta" Text="Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8 Word9 Word10 Word11 Word12 Word13 Word14 Word15 Word16 Word17 Word18 Word19 Word20" />
  21. </Grid>
  22. </StackLayout>

在代码隐藏中,

  1. private void btn2_SizeChanged(object sender, EventArgs e)
  2. {
  3. grid.RowDefinitions[0].Height = (sender as Button).Height;
  4. }

注意:第二行的标签也可以这样做

展开查看全部

相关问题