You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin中如何用XAML创建带居中文字的水平分隔线

实现带居中文字的分隔栏

你需要调整Grid的布局结构,改成一行三列的布局,让左右两侧的横线和中间的文字在同一行垂直居中,以下是可行的XAML代码:

<Grid Margin="10,0" VerticalOptions="Center">
    <!-- 定义三列:左右两列占剩余空间,中间列自适应文字宽度 -->
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <!-- 左侧横线 -->
    <BoxView Grid.Column="0" HeightRequest="1" BackgroundColor="White" VerticalOptions="Center"/>
    
    <!-- 中间文字 -->
    <Label Grid.Column="1" Text="OR" TextColor="White" Margin="10,0" VerticalOptions="Center"/>
    
    <!-- 右侧横线 -->
    <BoxView Grid.Column="2" HeightRequest="1" BackgroundColor="White" VerticalOptions="Center"/>
</Grid>

关键调整说明:

  • 把原来的两行布局改成一行三列,确保横线和文字在同一水平线上对齐
  • 左右两列用Width="*",自动均分剩余空间,保证两侧横线长度一致
  • 中间列用Width="Auto",仅占用文字所需宽度
  • 给Label添加左右Margin,让文字和横线之间保留合适间距
  • 给Grid和BoxView设置VerticalOptions="Center",确保所有元素垂直居中对齐

你可以根据页面风格需求,调整Margin、BackgroundColor、TextColor等属性。

内容的提问来源于stack exchange,提问作者Fxbrandon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 01:45:15