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>
但它只是导致了这个......
解决方案
原因:在 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>
推荐阅读
- javascript - 如何重绘然后删除旧版本的不规则形状?
- angular - Angular 在值更改时更新表单控制值
- sql - 如何在 git bash 中打开一个新选项卡?
- python - 在正则表达式中使用否定查找不起作用
- python - 在 app.on_event('startup') 中连接到数据库与在 FastAPI 中的依赖项之间的区别
- javascript - 添加列表(使用JS添加)后如何分配图标?
- css - 如果不使用背景大小,图像集不起作用:Firefox 88 上的封面
- python - 如何检查 y 变量是否受到任何 x 变量的影响?
- java - 如何使用 JDBC 为异常创建日志?
- c# - 将结果推送到构建器并在构建器完成时收到通知