android-设置LayoutParams以编程方式查看元素

android-设置LayoutParams以编程方式查看元素,android,android-layout,Android,Android Layout,我希望按钮有相同的大小和表是完全填补了按钮。但是按钮有不同的尺寸,我错在哪里 我的xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background"

我希望按钮有相同的大小和表是完全填补了按钮。但是按钮有不同的尺寸,我错在哪里

我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="horizontal" >

    <LinearLayout

    </LinearLayout>

    <TableLayout
        android:id="@+id/mainTableL"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="40dp" >
    </TableLayout>

</LinearLayout>
我的代码:

layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
mainTableLayout = (TableLayout) findViewById(R.id.mainTableL);

tableRow = new TableRow[VALUE_ROWS]; 
btn = new Button[VALUE_ROWS*VALUE_COLUMNS];

for (int indexRow = 0; indexRow < VALUE_ROWS; indexRow++){
    tableRow[indexRow] = new TableRow(this); 
    tableRow[indexRow].setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));


    for (int indexColumn = 0; indexColumn < VALUE_COLUMNS; indexColumn++){
        btn[countBtn] = new Button(this);
        btn[countBtn].setLayoutParams(layoutParams);
        btn[countBtn].setId(countBtn);
        btn[countBtn].setBackgroundResource(R.drawable.fon);
        tableRow[indexRow].addView(btn[countBtn],layoutParams);
        countBtn++;                 
    }
    mainTableLayout.addView(tableRow[indexRow], layoutParams);
}   
我为我的纯正英语感到抱歉。
谢谢

我不确定更改此选项是否会对您有所帮助,但这肯定应该得到修复。对于表行,应设置TableLayout.LayoutParams,而不是TableRow.LayoutParams:

mainTableLayout.addView(tableRow[indexRow]);
以后添加行时,不要指定layoutParams:

mainTableLayout.addView(tableRow[indexRow]);

还要注意,为行指定大小没有任何效果,它们的宽度设置为匹配父行和要换行的内容的高度(请参见)。与按钮类似,TableRow将其宽度设置为包裹内容,高度设置为匹配父级,而不管您在布局参数中指定了什么。

欢迎使用!如果答案对您有帮助,请随时接受: