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

Android 如何以编程方式将tableRow添加到Xamarin中的TableLayout,android,xamarin,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

因此,我的布局包含在我的布局中定义的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>

我在codebehind中访问它,并尝试向表行添加一个按钮,然后将该表行添加到表中:

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);
private TableLayout\u table
专用按钮
.
.
.
_table=findviewbyd(Resource.Id.table);

_button=new button(this){Text=“您需要为按钮使用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);

我已经对上面的代码进行了重构,因此它将是C语言而不是Java语言, 享受吧


如果您将代码转换为xml,则

<TableRow><Button /></TableRow>

因此,必须将layoutparams添加到以编程方式创建的每个视图中

 _button = new Button(this){Text = "<"};
_buttonparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.MatchParent);
_button.setLayoutParams(_buttonParams);
var tableRow = new TableRow(this);
LayoutParams _tableRowParams = new LayoutParams(-1,-2);
tableRow.setLayoutParam(_tableRowParams);
tableRow.AddView(_button, 0);
_table.AddView(tableRow, 0);

\u button=new button(这个){Text=“@robert watson你把这个修好了吗?
 _button = new Button(this){Text = "<"};
_buttonparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.MatchParent);
_button.setLayoutParams(_buttonParams);
var tableRow = new TableRow(this);
LayoutParams _tableRowParams = new LayoutParams(-1,-2);
tableRow.setLayoutParam(_tableRowParams);
tableRow.AddView(_button, 0);
_table.AddView(tableRow, 0);