使用单个TableRow和单个TextView创建单个字符的网格

时间:2013-08-23 06:32:34

标签: android textview tablelayout

我正在尝试创建像Android WordSearch应用程序,它在网格中显示一系列字母表。我最初使用GridView但无法横向滚动和放大一次垂直。因此切换到tablelayout。现在我想看看如下:

enter image description here

现在,当我使用TableLayout时,它只在一行中读取整个数据,只有一行。 enter image description here

我想知道我们是否可以遍历单个TableRow和单个TextView并创建前一个视图:以下是我的代码

         int index = 0;
                    TableLayout tableLayout = new TableLayout(getApplicationContext());
                    tableLayout.setColumnStretchable(2, true);
                    TableRow tableRow;
                    TextView textView;
                    for (int i = 0; i < 50; i++)  // this reads 50 chars from text file
                    {
                            tableRow = new TableRow(getApplicationContext());

                            for (int j = 0; j < 35; j++) //this creates 35 repetitions of above tablerow
                            {

                                    textView = new TextView(getApplicationContext());
                                    textView.setText(split3(numbers)[i]);
                                    textView.setTextSize(19);
                                    textView.setPadding(1, 2, 0, 2);
                                    textView.setTypeface(null, Typeface.BOLD);
                                    tableRow.addView(textView);
                                    index = index + 1;
                            }
                            tableLayout.addView(tableRow);
                    }

                    scrollView = new HScroll(GridActivity.this);
                    scrollView.addView(tableLayout);
                    VSC = new VScroll(GridActivity.this);
                    VSC.addView(scrollView);
                    setContentView(VSC);
            }

1 个答案:

答案 0 :(得分:0)

我终于设法自己解决了

        TextView textView = new TextView(getApplicationContext());
        tableRow = new TableRow(getApplicationContext());

        for (int i = 0; i < Math.round(numbers.length()/50); i++) // number of vertical rows
        { 
            tableRow = new TableRow(getApplicationContext()); 

            for (int j = 0; j < 45; j++) // no of max characters in a column  
            { 

                textView = new TextView(getApplicationContext()); 
                textView.setText(split3(numbers)[index]); 

                if (index == 13 || index == 15 || index == 17|| index == 19)
                {
                    textView.setTextColor(Color.RED);
                }
                else
                {
                    textView.setTextColor(Color.BLACK);
                }
                textView.setTextSize(21); 
                textView.setPadding(1, 1, 1, 1); 
                textView.setTypeface(null, Typeface.BOLD); 
                tableRow.addView(textView); 
                index = index+1; 
            } 

            tableLayout.addView(tableRow); 
        }

得到了我想要的东西。 enter image description here