如何编程在Xamarin通过tablerow添加到TableLayoutXamarin、tablerow、TableLayout

2023-09-06 09:05:20 作者:其实你很好只是我不配

所以,我有我的布局,其中包含我的布局定义这里TableLayout

So I have my layout that contains a TableLayout defined in my layout here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/defaultBackground_vert"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainLayout">
<TableLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/table">
</TableLayout>
</LinearLayout>

和我访问它在我的codebehind,并试图将按钮添加到表行和tablerow的添加到表:

And I am accessing it in my codebehind and trying to add a button to a table row and add that tablerow to the table:

private TableLayout _table
private Button _button
.
.
.
_table = FindViewById<TableLayout>(Resource.Id.table);
_button = new Button(this){Text = "<"};
_button = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
var tableRow = new TableRow(this);
tableRow.AddView(_button, 0);
_table.AddView(tableRow, 0);

问题是,tablerow的显示不出来,当我运行我的应用程序。

The problem is that the tableRow does not show up when I run my application.

推荐答案

您需要使用TableRow.Layoutparams的按钮..试试这个code。

You need to use TableRow.Layoutparams for the buttons.. Try this code.

    TableLayout _table = (TableLayout) findViewById(R.id.table);

    LayoutParams layoutParams = new TableRow.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    TableRow tableRow = new TableRow(this);

    Button _button = new Button(this);
    _button.setText(">>");
    _button.setLayoutParams(layoutParams);

    tableRow.addView(_button, 0);
    _table.addView(tableRow, 0);