简体   繁体   中英

Translating Xamarin Forms Grid Layout to Xamarin Android

Suppose I have the following grid in a .XAML file in a Xamarin Forms project:

<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>

How would I translate this XAML markup to Android XML? I have read the documentation for both the GridLayout and TableLayout views but I am not sure I completely understand the difference between them and, as such, not sure which one is the most appropriate for this scenario.

Also, I have a hard requirement that I must target Android API level 19. I mention this because it seems like there were items in the aforementioned documentation that indicated certain features were added in API level 21 or 22.

TableLayout extend LinearLayout ,there is no need to specify how many rows or columns the declaration contains,control the number of rows and columns in the table by adding TableRow /other components,it can't span rows and columns. It's not flexible

GridLayout added after Android 4.0,it can divide the entire container into grids for rows*columns,can set how many columns or rows a component spans

so GridLayout is better to use.

as you xaml have no content,if you use gridlayou ,you could write like this:

<?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>

and leftmargin , rightMargin and other properties of item can be used to set the horizontal and vertical spacing of item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM