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
相关产品推荐
相关产品推荐

