2012-12-02 214 views
2

我想創建一個基於字符串長度的gridView。在android中動態添加textView到gridView

每個TextView中應該表現出單個字符 - 下劃線 - 假設有字符串中沒有空間...

我以前沒有在C#中類似的東西 - 我craeted其中containes 15的tableview標籤,並根據字母的數量,移動他們每個人的權利。它看起來或多或少是這樣的:

public void setLetters(String str) 
     { 
      tableLayoutPanel1.Visible = false; 
      int x = revah(str); //revah returns the number of letters in the string 
      int yy = x/2; 
      int count = 0; 
      if (count <= x) 
      { 
       tableLayoutPanel1.SetColumn(lbl0, yy); 
       count++; 
      } 
      if (count <= x) 
      { 
       tableLayoutPanel1.SetColumn(lbl1, yy); 
       count++; 
      } 
      if (count <= x) 
      { 
       tableLayoutPanel1.SetColumn(lbl2, yy); 
       count++; 
      } 
      if (count <= x) 
      { 
       tableLayoutPanel1.SetColumn(lbl3, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl4, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl5, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl6, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl7, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl8, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl9, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl10, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl11, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl12, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl13, yy); 
      count++; 
     } 
     if (count <= x) 
     { 
      tableLayoutPanel1.SetColumn(lbl14, yy); 
      count++; 
     } 

     tableLayoutPanel1.Visible = true; 

    } 

我希望做的GridView使用android系統中類似的東西,但不知道如何甚至開始...幫助...

+0

如果textview的值是helloworld比你想在gridview中顯示helloworld像hll l o w o r l d嗎?我得到你嗎? – Zohaib

+0

嗯...不完全...但如果你告訴我該怎麼做,生病了解:) – omi

+0

你沒有看到任何方法讓你的代碼更簡單嗎? :) –

回答

1

按照這link 這將顯示如何使網格視圖,你現在想要什麼。你必須創建一個方法來讀取你的textview值,然後創建一個邏輯來將這些單詞分割成單個字符,然後在string []中添加這些字符。希望這會有所幫助

+0

偉大的鏈接... thanx! – omi

1

您可以創建一個使用GridView的佈局(通過活動填充 - http://developer.android.com/reference/android/app/Activity.html)以及使用textview的其他佈局。之後,你必須實現一個適配器來用字符串填充GridView(一個ArrayAdapter)(在方法getView中)。方法getCount()將確定項目的數量。這個方法在ArrayAdapter類中,你必須重寫它們。檢查這裏:http://developer.android.com/reference/android/widget/ArrayAdapter.html

+0

謝謝...真的需要學習如何使用arrayAdapter ... – omi