и так код проекта:
MainActivity:
Код: Выделить всё
package com.example.caltest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Table(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Код: Выделить всё
package com.example.caltest;
import android.content.Context;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
public class Table extends TableLayout {
private LayoutParams rowLayoutParams = null;
private LayoutParams tableLayoutParams = null;
public Table(Context context) {
super(context);
setLayoutParams(getTableLayoutParam());
addRows(context);
}
private void addRows(Context context) {
for (int i = 0; i < 6; i++) {
TableRow row = new TableRow(context);
row.setLayoutParams(getRowLayoutParams());
row.setGravity(Gravity.CENTER);
for (int j = 0; j < 7; j++) {
Cell cell = new Cell(context, getTableLayoutParam());
cell.setCellText(i + j);
/*
* Собственно трабл здесь: если пишу
* row.addView(cell);
* то все нормально но если
* row.addView(cell, getRowLayoutParams());
* то выходит пустая вьюха. под дебагом запусказ,
* ставил маркер смотрел в памяти вьюха висит
* а на экране её нету.
* тотже эффект если в этом месте создать LayoutParam
* и задать для него Width и Height в MATCH_PARENT
* */
row.addView(cell);
}
addView(row, getTableLayoutParam());
}
}
private LayoutParams getRowLayoutParams() {
if (rowLayoutParams == null) {
rowLayoutParams = new LayoutParams();
rowLayoutParams.width = LayoutParams.MATCH_PARENT;
rowLayoutParams.height = LayoutParams.WRAP_CONTENT;
// rowLayoutParams.gravity = Gravity.CENTER;
}
return rowLayoutParams;
}
private LayoutParams getTableLayoutParam() {
if (tableLayoutParams == null) {
tableLayoutParams = new LayoutParams();
tableLayoutParams.weight = LayoutParams.MATCH_PARENT;
tableLayoutParams.height = LayoutParams.WRAP_CONTENT;
tableLayoutParams.weight = 1;
// tableLayoutParams.gravity = Gravity.CENTER;
}
return tableLayoutParams;
}
}
Код: Выделить всё
package com.example.caltest;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Cell extends LinearLayout {
TextView text = null;
public Cell(Context context, LayoutParams cellLayoutParams) {
super(context);
addView(getTextView(context), cellLayoutParams);
}
private View getTextView(Context context) {
text = new TextView(context);
text.setGravity(Gravity.CENTER);
text.setTextSize(28);
return text;
}
public void setCellText(int i) {
text.setText(String.valueOf(i));
}
}
Пишу row.addView(cell) и все видно, пишу row.addView(cell, getRowLayoutParams()); или тутже создаю новый объект параметров и передаю, то экран пустой.... ни как не могу догнать что за магия...