1. 程式人生 > >xamarin.form grid控制元件

xamarin.form grid控制元件

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App1"
             x:Class="App1.MainPage">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="100"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="100"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Label Text="1" Grid.Row="0" Grid.Column="0"></Label>
        <Label Text="2" Grid.Row="0" Grid.Column="1"></Label>
        <Label Text="3" Grid.Row="0" Grid.Column="2"></Label>
        
        <Label Text="4" Grid.Row="1" Grid.Column="0"></Label>
        <Label Text="5" Grid.Row="1" Grid.Column="1"></Label>
        <Label Text="6" Grid.Row="1" Grid.Column="2"></Label>
        
        <Label Text="7" Grid.Row="2" Grid.Column="0"></Label>
        <Label Text="8" Grid.Row="2" Grid.Column="1"></Label>
        <Label Text="9" Grid.Row="2" Grid.Column="2"></Label>
    </Grid>
</ContentPage>

  重點:

RowDefinitions 表格控制元件有多少個行
<RowDefinition Height="Auto"></RowDefinition> 定義一行,並且高度自動,Height有3種引數Auto和*和數字100
ColumnDefinitions 表格控制元件有多少個列
<ColumnDefinition Width="Auto"></ColumnDefinition> 定義一列,並且高度自動,Width有3中引數Auto和*和數字
Grid.Row="0" Grid.Column="0" 當前控制元件放到表格的第幾行第幾列中

使用程式碼建立Grid
Grid g = new Grid();
            g.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Auto) });
            g.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(10, GridUnitType.Star) });
            g.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(100, GridUnitType.Absolute) });
            g.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(0, GridUnitType.Auto) });
            g.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Star) });
            g.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100, GridUnitType.Absolute) });
            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    Label l1 = new Label() { Text = ""+x+y };
                    g.Children.Add(l1);

                    Grid.SetRow(l1, x);
                    Grid.SetColumn(l1, y);
                }
            }
            this.Content = g;