嘗試在網格佈局上顯示網格。是否有代碼顯示網格到任何佈局?
似乎無法找到一種方法讓它顯示... 任何人都知道任何代碼來顯示網格的佈局,這已經被做出來了嗎?
這裏是我當前的代碼:
public class Activity1 : Activity
{
public StackLayout MainStackLayout { get; set; }
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
MainStackLayout = new StackLayout();
GridView Grid = FindViewById<GridView>(Resource.Id.gridView1);
Grid g = new Grid { ColumnSpacing = 0, BackgroundColor = Color.Gray };
g.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
g.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Absolute) });
g.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
g.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
g.RowDefinitions.Add(new RowDefinition { Height = new GridLength(30, GridUnitType.Absolute) });
g.Children.Add(new Label { Text = "Test" }, 0, 0);
g.Children.Add(new Label { Text = "Test" }, 1, 0);
g.Children.Add(new Label { Text = "Test" }, 2, 0);
g.Children.Add(new Label { Text = "Test" }, 3, 0);
g.Children.Add(new Label { Text = "Test" }, 0, 1);
g.Children.Add(new Label { Text = "Test" }, 1, 1);
g.Children.Add(new Label { Text = "Test" }, 2, 1);
g.Children.Add(new Label { Text = "Test" }, 3, 1);
//Display here
}
}
。
Layout1.axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background1"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/relativeLayout1" />
感謝所有幫助:)
uhhm。你使用'Xamarin.Forms'還是普通的'Xamarin.Android'? 'layout1.axml'看起來像什麼?你不顯示提示。你只是在創建視圖。您必須將它們添加到父視圖。 –
@ Sven-MichaelStübe感謝您的回覆,我正在使用Xamarin.Forms。此外,現在layout1.axml只是默認值。我把它的代碼放在主要問題中。我對android非常陌生,有關如何將它們添加到父視圖的任何提示? –