0

几个小时以来,我一直坐在我的头上,但我还没有找到解决这个奇怪问题的方法。我的要求是我需要动态创建一个带有标题和内容的表格布局。

这是我用来动态生成表格布局的代码。

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 上清晰可见。

模拟器 - 截图

4

2 回答 2

0

尝试为其添加权重 1:

tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1));

在 TableRow 上设置权重解决了我最近看到的另一个布局问题。所以,值得一试。

于 2013-11-08T21:41:07.053 回答
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();
于 2013-11-08T07:17:31.613 回答