大家好!我有这个 TableLayout,创建后屏幕是空的。由于行数和列数是手动给出的规范,我没有 xml 文件。首先,我必须用空的 1 x 1 不可见的 TextView-s 填充表格,因为如果我想在 k <=rows 和 l <=columns 的坐标 (k, l) 处添加一个按钮,则必须使用此解决方案。代码如下:
package hu.harge;
import java.awt.Button;
import javax.swing.text.TableView.TableRow;
import sun.jkernel.Bundle;
public class AccessibleLayoutActivity extends Activity {
private int rows, columns;
private TableLayout tl = null;
private ScrollView sv;
public void createView(int rows, int columns) {
// TODO Auto-generated method stub
this.rows = rows;
this.columns = columns;
this.sv = new ScrollView(this);
tl = new TableLayout(this);
for (int i = 0; i < rows; i++) {
TableRow tr = new TableRow(this);
for (int j = 0; j < columns; j++) {
TextView tv = new TextView(this);
tr.addView(tv);
tv.setMaxHeight(1);
tv.setMaxWidth(1);
}
tl.addView(tr);
}
sv.addView(tl, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
this.setContentView(sv);
}
public void putButton(int row, int column, final String label) {
if (tl != null) {
// TODO Auto-generated method stub
if (row <= rows && column <= columns) {
Button b = new Button(this);
b.setText(label);
TableRow tr = (TableRow) tl.getChildAt(row - 1);
tr.removeViewAt(column - 1);
tr.addView(b, column - 1, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createView(6, 3);
putButton(1, 3, "Exit");
putButton(2, 1, "Menu");
}
}
我认为 ListView 不能解决我的问题。
问题是屏幕上什么都没有。
感谢您的回答:Geri