如何在Xamarin中以编程方式将tableRow添加到TableLayout

Rob*_*son 3 android xamarin

所以我的布局包含我的布局中定义的TableLayout

<?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>
Run Code Online (Sandbox Code Playgroud)

我在我的代码隐藏中访问它并尝试向表行添加一个按钮并将该tablerow添加到表中:

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);
Run Code Online (Sandbox Code Playgroud)

问题是当我运行我的应用程序时tableRow不会显示.

小智 10

您需要使用TableRow.Layoutparams作为按钮..尝试此代码.

    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);
Run Code Online (Sandbox Code Playgroud)