2013-01-08 63 views
0

我有一個文本文件,我想在文本視圖的數量中顯示它的文本,我在運行時設置了它們的文本。拆分文本,在文本視圖中使文本視圖爲false

這裏是我的代碼,我用我的程序:

while(text.length() > 0) 
    { 
     text = text.trim(); 
     int index = text.indexOf("L1"); 
     int index2 = 0; 
     temp = ""; 
     if(index > -1 && index == 0) 
     { 
      index2 = text.indexOf("/L1"); 
      temp = text.substring(index + 2, index2); 
      temp.trim(); 
      t = temp.toCharArray(); 
      text = text.substring(index2 + 3, text.length()); 
      index = text.indexOf("\r\n"); 
      while(index > -1 && index == 0) 
      { 
       temp += "\r\n"; 
       text = text.substring(index + 2, text.length()); 
       index = text.indexOf("\r\n"); 
      } 
      temp = String.valueOf(t); 
     } 
     else 
     { 
      index = text.indexOf("L2"); 
      if(index > -1 && index == 0) 
      { 
       index2 = text.indexOf("/L2"); 
       temp = text.substring(index + 2, index2); 
       text = text.substring(index2 + 3, text.length()); 
       index = text.indexOf("\r\n"); 
       while(index > -1 && index == 0) 
       { 
        temp += "\r\n"; 
        text = text.substring(index + 2, text.length()); 
        index = text.indexOf("\r\n"); 
       } 
      } 
      else 
      { 
       index = text.indexOf("List"); 
       if(index > -1 && index == 0) 
       { 
        index2 = text.indexOf("/List"); 
        temp = text.substring(index + 4, index2); 
        text = text.substring(index2 + 5, text.length()); 
        index = text.indexOf("\r\n"); 
        while(index > -1 && index == 0) 
        { 
         temp += "\r\n"; 
         text = text.substring(index + 2, text.length()); 
         index = text.indexOf("\r\n"); 
        } 
       } 
       else 
       { 
        text = text.trim(); 
        index = text.indexOf("Plain"); 
        if(index > -1 && index == 0) 
        { 
         index2 = text.indexOf("/Plain"); 
         temp = text.substring(index + 5, index2); 
         text = text.substring(index2 + 6, text.length()); 
         index = text.indexOf("\r\n"); 
         while(index > -1 && index == 0) 
         { 
          temp += "\r\n"; 
          text = text.substring(index + 2, text.length()); 
          index = text.indexOf("\r\n"); 
         } 
        } 
       } 
      } 
     } 

    } 
    for(int i = 0; i < texts.length; i++) 
    { 
     TextView tv = new TextView(this); 
     tv.setText(Farsi.Convert(texts[i])); 
     Typeface font = Typeface.createFromAsset(getAssets(), "fonts/naz.ttf"); 
     tv.setTypeface(font); 
     tv.setTextColor(Color.BLACK); 
     tv.setGravity(Gravity.RIGHT); 
     tv.setPadding(10, 10, 10, 10); 
     tv.setLayoutParams(params); 
     linearLayout.addView(tv); 
    } 
    layout.addView(linearLayout); 
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    this.addContentView(layout, p); 
} 

我用在文本文件中的一些原因,一些輔助詞(如「L1」和「/ L1」),即刪除在運行時間。但是,當我運行該程序時,在輸出文本視圖中顯示的文本不是正確的風格,我想對齊它的權利,但在輸出中有一些額外的文本,這是不是在輸入文本文件。

在下面的2張圖片中,我在輸入文本文件中附加了帶和不帶輔助字的輸出。

輸出與助詞:

output with auxiliary words:

無需輔助字輸出,在這種狀態下,我使用「的setContentView」功能設置佈局的內容,並使用讀取整個文件,而不是分割文本輔助詞索引。在這種狀態下,輸入不包含輔助字。

output without auxiliary words

問題是什麼朋友? 謝謝

回答

1

首先:
使用String.split()拆分您的字符串。

它的工作原理是這樣的,如果你有一個字符串A,B,C並且你使用myString.split(,)輸出將是一個子串的數組,即[A,B,C]。
但是這對你的情況很有用,如果你可以有相同的分隔符,即L1和L2是相同的。

第二:您可以在layout.xmltextview中使用android:gravity="right"。這將解決字符串向左對齊的問題。

+0

感謝您的回答,但我設置了這條線的重力:「tv.setGravity(Gravity.RIGHT);」,但它不適合我!問題是,當我使用子字符串函數,並使用這些文本創建新的文本視圖時,輸出將是這樣的輸出圖像在帖子中 – user1793700