2013-08-23 16 views
0

我正在嘗試創建類似Android WordSearch應用程序的地方,它在網格中顯示一系列字母。我最初使用GridView,但不能同時垂直水平滾動&。因此切換到tablelayout。現在我想給這個樣子的,如下:使用單個TableRow和單個TextView創建單個字符的網格

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); 
      } 
+0

您爲表格行或表格佈局設置了哪個方向? – Piyush

+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

+0

真的很好..........你是.. – Piyush

相關問題