Hy all! 我有这个TableLayout,创建后屏幕为空。我没有xml文件,因为规范是手动给出行数和列数。首先,我必须用空的1 x 1不可见的TextView-s填充表,因为如果我想在cordinates(k,l)添加一个按钮,其中k< = rows和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无法解决我的问题。
问题是屏幕上没有任何东西闪耀。
感谢您的回答: 格日
答案 0 :(得分:0)
我在这里看到很多错误: 首先,
tr.addView(b, column - 1, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
应改为
tr.addView(b, column - 1, new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
根据元素父级的类,layoutparams类应始终具有此类前缀。
此外,我没有看到你为TableRow或桌子或任何电视设置任何布局。任何视图都应设置高度和宽度。总是。
如果它没有帮助(你也可能有其他错误),最好从XML开始使用布局,使它们工作,然后逐个将它们移动到代码中。并检查每一步的功能。还要给所有元素一些背景来看它们。并且将它们调试得更大一些 - 再次,看到它们更好......作为红帽中的狼: - )
答案 1 :(得分:0)
Doc说: TableRow的子节点不需要在XML文件中指定layout_width和layout_height属性。 TableRow始终将这些值分别强制为MATCH_PARENT和WRAP_CONTENT。