我试图在没有XML的情况下创建TableLayout
,屏幕上没有任何内容。以下是我的代码。请让我知道这是什么错误?
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act);
TableLayout tlMain =(TableLayout)findViewById(R.id.tlMain);
LayoutInflater inflater = getLayoutInflater();
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
tlMain.setLayoutParams(lp);
tlMain.setStretchAllColumns(true);
tlMain.setShrinkAllColumns(true);
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
rowLp.weight=1;
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
for(int i=0;i<4;i++)
{
TableRow tableRow = new TableRow(this);
for (int j = 0; j < 4; j++) {
TextView number2 = new TextView(this);
number2.setTextColor(color.black);
number2.setText(String.valueOf(i));
tableRow.addView(number2,cellLp);
tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
}
tlMain.addView(tableRow,rowLp);
}
setContentView(tlMain);
}
答案 0 :(得分:0)
变化:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
到
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
也不要打电话:
setContentView(tlMain);
假设您在R.layout.act
尝试改变:
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
为:
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
我的猜测是你有一个宽度为Wrap_content的表行和一个宽度为fill_parent的Textview。
这是矛盾的陈述,而android无法绘制它。
答案 1 :(得分:0)
如果您想在不使用TableLayout
的情况下创建layout-xml
,那么
试试这个
TableLayout tlMain = new TableLayout(this);
而不是
setContentView(R.layout.act);
TableLayout tlMain =(TableLayout)findViewById(R.id.tlMain);
答案 2 :(得分:0)
而不是使用
setContentView(tlMain);
使用
mainlayout.addView(tlMain);
其中mainlayout可以是线性或相对布局,也可以是包装表格的任何其他容器。
答案 3 :(得分:0)
This is How it started Working.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout table = new TableLayout(this);
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
TableRow rowTitle = new TableRow(this);
rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow.LayoutParams trparams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT);
rowTitle.setLayoutParams(trparams);
rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow rowDayLabels = new TableRow(this);
TableRow rowAmounts = new TableRow(this);
TextView empty = new TextView(this);
empty.setText("Empty");
TextView title = new TextView(this);
title.setLayoutParams(trparams);
table.setLayoutParams(trparams);
title.setText("WikiCode");
rowTitle.addView(title);
rowTitle.addView(empty);
table.addView(rowTitle);
for(int i=0;i<4;i++)
{
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(trparams);
for (int j = 0; j < 4; j++) {
TextView number2 = new TextView(this);
number2.setLayoutParams(trparams);
number2.setText("check"+i);
tableRow.addView(number2);
tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
}
table.addView(tableRow);
}
setContentView(table);
}