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);
}
TableLayout Without XML, Nothing Appears on Screen
-
06-10-2022 - |
Вопрос
I was trying to create TableLayout
Without XML, Nothing Appears on Screen. Below is my code. Please let me know what is the mistake?
Code :
@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);
}
Решение 4
Другие советы
If you want to create TableLayout
without using layout-xml
then
Try with this
TableLayout tlMain = new TableLayout(this);
Instead of
setContentView(R.layout.act);
TableLayout tlMain =(TableLayout)findViewById(R.id.tlMain);
Instead of using
setContentView(tlMain);
use
mainlayout.addView(tlMain);
where mainlayout could be linear or relative layout or any other container, which wrap your table.
change:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
to
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
also don't call:
setContentView(tlMain);
Assuming that you have a table layout in R.layout.act
Try to change:
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
to:
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
My guess is that you have a table row with width Wrap_content and a Textview inside it with width fill_parent.
This are contradictory statements and android is not able to draw it.
Не связан с StackOverflow