Hy都! 我有这个TableLayout和创建后屏幕是空的。我没有xml文件,因为规定行和列的数量是手动的。首先我必须用空的1 x 1不可见的TextView-s填充表格,因为如果我想在坐标(k,l)处添加一个按钮,则需要使用此解决方案,其中k < =行和l < =列。 代码如下:动态TableLayout在Android中的刷新
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控件不能成为我的问题的解决方案,我认为。
问题是,没有什么东西闪耀在屏幕上。
感谢您的答案: 杰瑞哈莉
那么,你的实际问题是什么? P.S .:能否请您在未来更好地设计您的代码格式?我只是修复了这个帖子。 – henrik 2012-01-29 16:13:37
@亨里克你说得对,问题的描述不完全可以理解,所以我纠正了它。 – Viceman3 2012-01-29 20:43:26