我几个小时以来一直坐在我的头上,但我还没有找到解决这个奇怪问题的方法。我的要求是我需要动态创建包含标题和内容的表格布局。
这是我用来动态生成表格布局的代码。
TableLayout tableLayout = (TableLayout)findViewById(R.id.tabContentLayout);
tableLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
tableLayout.removeAllViews();
for(int i=0;i<rows.length;i++){
Log.d("Rows",rows[i]);
String row = rows[i];
TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setBackgroundResource(R.drawable.cell_shape);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
final String[] cols = row.split(";");
Log.i(LOG_CLASS, "<<---- Columns count : " + cols.length + " ---->>");
for (int j = 0; j < cols.length; j++) {
final String col = cols[j];
TextView columsView = new TextView(getApplicationContext());
columsView.setBackgroundResource(R.drawable.cell_header);
columsView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
columsView.setTextColor(color.white);
columsView.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
columsView.setGravity(Gravity.CENTER);
columsView.setText(String.format("%7s", col));
Log.d("Cols", String.format("%7s", col));
tableRow.addView(columsView);
}
// tableLayout.addView(tableRow,new TableLayout.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
tableLayout.addView(tableRow);
}
tableLayout.requestLayout();
行是String[]
,其中包含由;
例如:Apple;10/25/2013;10/25/2014;Srivatsa
最新更新: 事实上,内容视图在模拟器上隐约可见。(但这在手机上看不到。)现在我需要的是让文本在UI上显得清晰。
答案 0 :(得分:0)
// try this
TableLayout tableLayout = (TableLayout)findViewById(R.id.tabContentLayout);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
tableLayout.removeAllViews();
for(int i=0;i<rows.length;i++){
Log.d("Rows",rows[i]);
String row = rows[i];
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundResource(R.drawable.cell_shape);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
final String[] cols = row.split(";");
Log.i(LOG_CLASS, "<<---- Columns count : " + cols.length + " ---->>");
for (int j = 0; j < cols.length; j++) {
final String col = cols[j];
TextView columsView = new TextView(getApplicationContext());
columsView.setBackgroundResource(R.drawable.cell_header);
columsView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
columsView.setTextColor(color.white);
columsView.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
columsView.setGravity(Gravity.CENTER);
columsView.setText(String.format("%7s", col));
Log.d("Cols", String.format("%7s", col));
tableRow.addView(columsView);
}
tableLayout.addView(tableRow);
}
tableLayout.requestLayout();
答案 1 :(得分:0)
尝试为其添加1的权重:
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1));
在TableRow上设置权重解决了我最近看到的另一个布局问题。所以,值得一试。