android - 将 Xamarin 表单网格布局转换为 Xamarin Android
问题描述
假设我在 Xamarin Forms 项目的 .XAML 文件中有以下网格:
<Grid ColumnSpacing="12">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
</Grid>
我如何将此 XAML 标记转换为 Android XML?我已经阅读了GridLayout
和TableLayout
视图的文档,但我不确定我是否完全理解它们之间的区别,因此不确定哪个最适合这种情况。
另外,我有一个硬性要求,即我必须针对 Android API 级别 19。我之所以提到这一点,是因为上述文档中似乎有一些项目表明某些功能是在 API 级别 21 或 22 中添加的。
解决方案
TableLayout 扩展LinearLayout
,不需要指定声明包含多少行或列,通过添加TableRow
/其他组件来控制表格的行数和列数,它不能跨越行和列。它不灵活
Android 4.0后添加的GridLayout,可以将整个容器划分为行*列的网格,可以设置一个组件跨越多少列或多少行
所以GridLayout
更好用。
因为你的 xaml 没有内容,如果你使用 gridlayu ,你可以这样写:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="4"
android:id="@+id/root">
//content components
...
</GridLayout>
和leftmargin
,rightMargin
和 item 的其他属性可以用来设置 item 的水平和垂直间距
推荐阅读
- python - Renpy - 前面的标记语句不需要块。请检查此行的缩进
- docker - 如何管理动态 docker 实例
- scala - 如何使用 Scalajs 0.6 和 1.0.0 交叉编译 sbt 顶级项目?
- android - (android.view.View$OnClickListener)' 在一个空对象引用 Kotlin
- angular - 在循环中请求 http.post 在 Angular 中不起作用
- python - 为什么 logger.info() 只在调用 logging.info() 后出现?
- python - 如何使用 Python 中的参数运行 PowerShell 脚本
- java - 选中复选框时,如何使用 e.getStateChanged 获取分配给它的文本?
- pandas - 当因变量的变化非常小时,使用什么指标来定义模型性能?
- python - 合并递归排序的链表未运行