Zac*_*ell 24 c# xamarin xamarin.forms
我在Xamarin.Forms中构建一个网格.而且我想添加像桌子这样的边框.我认为我可以在定义行和列时添加边框,但是失败了.谁能帮我?这是我目前的代码.
Grid grid = new Grid {
VerticalOptions = LayoutOptions.FillAndExpand,
RowDefinitions = {
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
},
ColumnDefinitions = {
new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength (5, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) },
}
};
Run Code Online (Sandbox Code Playgroud)
Dan*_*rda 28
没有Border财产GridView,但是:
只需设置grid.BackgroundColor所需的边框颜色值,然后设置grid.ColumnSpacing并设置grid.RowSpacing为某个值,并确保添加到网格中的所有控件都已BackgroundColor正确设置.
Stu*_*rla 27
这是完整的答案(在XAML中),无需编写自定义渲染器或效果.
代码有点冗长但易于理解,结果就像在图像上一样
这是将边框放在网格上的代码(更多的是你可以完全控制它们,就像你注意到最左边没有蓝线)
<Grid BackgroundColor="White">
<Grid.RowDefinitions>
<RowDefinition Height="1"/>
<RowDefinition Height="15"/>
<RowDefinition Height="1"/>
<RowDefinition Height="15"/>
<RowDefinition Height="1"/>
<RowDefinition Height="15"/>
<RowDefinition Height="1"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="1" />
</Grid.ColumnDefinitions>
<BoxView Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="8" BackgroundColor="Red" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand"/>
<!--Your stuff here!-->
<BoxView Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="8" BackgroundColor="Red" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand"/>
<!--Your stuff here!-->
<BoxView Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="8" BackgroundColor="Red" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand"/>
<!--Your stuff here!-->
<BoxView Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="8" BackgroundColor="Red" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand"/>
<!--Vertical lines and no "stuff"-->
<BoxView Grid.Column="1" Grid.Row="0" Grid.RowSpan="7" BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End"/>
<BoxView Grid.Column="3" Grid.Row="0" Grid.RowSpan="7" BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End"/>
<BoxView Grid.Column="5" Grid.Row="0" Grid.RowSpan="7" BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End"/>
<BoxView Grid.Column="7" Grid.Row="0" Grid.RowSpan="7" BackgroundColor="Blue" WidthRequest="1" VerticalOptions="FillAndExpand" HorizontalOptions="End"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
Abd*_*han 10
<Grid BackgroundColor="White" >
<BoxView BackgroundColor="Pink" />
<Grid BackgroundColor="White" Margin="5">
</Grid>
</Grid>
Run Code Online (Sandbox Code Playgroud)
刚刚注意到我的例子类似于Sturla,但有点不同所以我会把它留下来.
代码并不是非常漂亮,但我做了类似的事情,BoxView在每列之间添加了1px ,然后在你Grid的底部添加1个,在你的底部添加1个Grid,如下所示:
<Grid VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
RowSpacing="0"
ColumnSpacing="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1"/>
</Grid.RowDefinitions>
<BoxView BackgroundColor="Black"
HeightRequest="1"
HorizontalOptions="FillAndExpand"
Grid.Row="0"/>
<Grid VerticalOptions="Start"
ColumnSpacing="0"
Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="1"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Text="Button 1"/>
<BoxView BackgroundColor="Black"
WidthRequest="1"
VerticalOptions="FillAndExpand"
Grid.Column="1"/>
<Button Text="Button 1"
Grid.Column="2"/>
</Grid>
<BoxView BackgroundColor="Black"
HeightRequest="1"
HorizontalOptions="FillAndExpand"
Grid.Row="2"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
*编辑:自写这篇文章以来,我改变了我的方式.现在,就像Daniel Luberda的回答一样,我只需设置Grid.BackgroundColor为Color.Black,然后我就可以删除所有BoxViews并完成了.我这样做是因为我认为在屏幕上显示几个视图要好得多,特别是如果你在上面放一些类似的东西ListView.
此外,由于很多页面会Button在页面加载(使用ScaleTo())时为s 设置动画,我最初设置Grid.BackgroundColor为Color.Transparent或者Color.White动画完成后再将其更改为Color.Black.到目前为止工作得很好.
| 归档时间: |
|
| 查看次数: |
44436 次 |
| 最近记录: |