xamarin - Xamarin Forms 中大小相同的标签

标签 xamarin xamarin.forms

使用水平 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>

但这只是导致了这个......

enter image description here

最佳答案

原因:当你在 stackLayout 中添加标签时,stackLayout 不会使 subview 适合大小。

解决方案:

将标签放在网格中。引用以下代码。

<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>

关于xamarin - Xamarin Forms 中大小相同的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54817673/

相关文章:

ios - 为什么在我尝试更新我的解决方案时 Xamarin Studio 说 "version control operation failed due to unmerged path error"?

c# - 在 Xamarin android (xamarin.android.dll) 中使用 System.Web

android - 可移植移动/平板电脑应用程序概念验证

xaml - ScrollView 中的 Xamarin Center StackLayout

xamarin - Droid Library 认为它是可穿戴的 如何让它不可穿戴?

xaml - 使用带有标签可见性 Xamarin 表单的转换器时如何将默认值设置为 false

listview - 如何使用 x :Name of items in listView at codebehind?

c# - 通过网络请求传输图像

xamarin.forms - 在 Xamarin.Forms 中使用 PCLStorage 读取文本文件

c# - 菜单的 MVVM 最佳实践