【问题标题】:Equal sized labels in Xamarin FormsXamarin Forms 中相同大小的标签
【发布时间】:2019-02-22 08:59:19
【问题描述】:

使用水平 StackLayout,我想在屏幕上显示 3 个等宽的标签。我不想使用 WidthRequest 属性,而是希望每个标签的大小相同,内容以“框”为中心。我希望标签根据它们运行的​​设备调整大小。所以 3 个标签,宽度相等,无论设备如何。

我知道这可以使用 Grid (Width="Auto") 来完成,但可以使用水平对齐的 StackLayout 吗?

我认为这会起作用......

<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
    <Label Text="aaaa" 
            HorizontalOptions="FillAndExpand" 
            HorizontalTextAlignment="Center" 
            BackgroundColor="Blue" />
    <Label Text="aaaaaaaaaaaa" 
            HorizontalOptions="FillAndExpand" 
            HorizontalTextAlignment="Center" 
            BackgroundColor="Green" />
    <Label Text="aa" 
            HorizontalOptions="FillAndExpand" 
            HorizontalTextAlignment="Center"  
            BackgroundColor="Red" />
</StackLayout>

但结果就是这样……

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    原因: 在 stackLayout 中添加标签时,stackLayout 不会使子视图适合大小。

    解决方案:

    将标签放在网格中。参考以下代码。

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
    
        <Label  Grid.Row="0" Grid.Column="0"  Text="aaaa" BackgroundColor="Blue" />
        <Label  Grid.Row="0" Grid.Column="1"  Text="aaaaaaaaaaaa" BackgroundColor="Green"/>
        <Label  Grid.Row="0" Grid.Column="2"  Text="aa"  BackgroundColor="Red" />
    
    </Grid>
    

    【讨论】:

      猜你喜欢
      • 2018-03-12
      • 1970-01-01
      • 2014-10-05
      • 2021-12-03
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 2020-08-24
      • 2019-12-02
      相关资源
      最近更新 更多