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

问题描述 投票:0回答:1

我的网格内有两个按钮,其中一个包含较长的文本,无法在一行上显示。带有长文本的按钮似乎高度不正确,并且某些文本是不可见的。从我所看到的来看,如果使用屏幕的整个宽度(与网格外的按钮相比),高度就是它所需要的高度。标签的行为似乎类似。

这是代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1.MainPage">

    <StackLayout>
        <Button Text="1 line" />
        <Button Text="Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8 Word9 Word10 Word11 Word12 Word13 Word14 Word15 Word16 Word17 Word18 Word19 Word20" />
        <Grid BackgroundColor="Cyan">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Button Grid.Row="0" Grid.Column="0" Text="1 line" />
            <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" />

            <Label Grid.Row="1" Grid.Column="0" BackgroundColor="Green" Text="1 line" />
            <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" />
        </Grid>
    </StackLayout>

</ContentPage>

这是结果:

我是否在网格定义中遗漏了某些内容,无法正确测量其中的元素?

xamarin xamarin.forms controls
1个回答
0
投票

尝试在 StackLayout 中添加 Button

<StackLayout>
    <Button Text="1 line" />
    <Button Text="Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8 Word9 Word10 Word11 Word12 Word13 Word14 Word15 Word16 Word17 Word18 Word19 Word20" />
    <Grid x:Name="grid" BackgroundColor="Cyan">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <StackLayout Grid.Row="0" Grid.Column="0">
            <Button x:Name="btn1"  Text="1 line" VerticalOptions="Start"/>
        </StackLayout>
        <StackLayout Grid.Row="0" Grid.Column="1">
            <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"/>
        </StackLayout>
        <Label Grid.Row="1" Grid.Column="0" BackgroundColor="Green" Text="1 line" />
        <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" />
    </Grid>
</StackLayout>

在代码隐藏中

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

© www.soinside.com 2019 - 2024. All rights reserved.