2012-03-22 57 views
0

我需要在從AsyncTask遠程檢索到文本後向ScrollableLayout添加文本。由於我不知道涉及的字符串的數量,我需要以編程方式根據需要創建儘可能多的字符串。在AsyncTask的末尾添加TextViews

佈局代碼

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/view_phone_mainView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:id="@+id/view_phone_linearLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="top" > 

     <TextView 
      android:id="@+id/view_phone_lblTitle" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal|center_vertical" 
      android:text="@string/view_phone_title" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <TextView 
      android:id="@+id/view_phone_lblWarning" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/view_phone_warning" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:visibility="invisible" /> 

    </LinearLayout> 

</ScrollView> 

活動

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     pDialog = new ProgressDialog(this); 
     pDialog.setIndeterminate(true); 
     pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     pDialog.setMessage(getString(R.string.view_phone_wait)); 

     pDialog.show(); 

     task = new MessageLoaderTask(); //Returns String[] on callback 
     task.execute((Void) null); 
    } 

    public void onRulesLoaded(String[] messages) { //Directly called by OnPostExecute 
     LinearLayout container = (LinearLayout) findViewById(R.id.view_phone_linearLayout); 
     if (messages != null) { 

      for (String m : messages) { 
       TextView tv = new TextView(this); 
       tv.setText(m); 
       tv.setTextAppearance(this, android.R.attr.textAppearanceSmall); 
       tv.setVisibility(View.VISIBLE); 
       tv.setBackgroundColor(Color.TRANSPARENT); 
       tv.setTextColor(Color.BLACK); 

       container.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
      } 
     } 

     pDialog.dismiss(); 
    } 

我相信,通過調試,該字符串正確估值。結果是標題標籤被顯示,警告被隱藏,但下面只有白色......

我試着滾動並注意到滾動區域是veeeeeeeeeery長,兼容長存根Lorem ipsum存根我用於測試的文本。如果我通過調試截斷其中一個字符串爲空(只有2個),則可滾動區域的高度較短。我使用輕的主題,所以我期望在白色背景上的黑色文字。

tv.setTextAppearance(this, android.R.attr.textAppearanceSmall); 
tv.setVisibility(View.VISIBLE); 
tv.setBackgroundColor(Color.TRANSPARENT); 
tv.setTextColor(Color.BLACK); 

在第二次增加時,一切都失敗了。沒有區別,無論他們是否在位。我能做些什麼來解決?

感謝

+0

我不明白你有一串字符串?所以對於這些字符串數組中的每一個,您都可以同步創建一個textview,並且在每個textview中按順序排列一個字符串? – sdfwer 2012-03-22 17:23:05

+0

對於數組中的每個字符串,我需要一個TextView。好的,我可以將所有字符串合併爲\ n \ n,並只使用一個TextView – 2012-03-22 17:38:39

回答

1

不知道爲什麼你會滾動型的長度增加,如果這是唯一的問題,但似乎你需要在你的LinearLayout的方向設置爲垂直。

+0

我相信你是對的! :) – 2012-03-22 17:45:30

0

使用下面的代碼。

<EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textMultiLine" > 

     <requestFocus /> 
    </EditText> 

通過在字符串之間保留'\ n',將所有字符串添加到此EditText。這樣每件事情都會正確對齊。

+0

對不起,我不需要一個文本框,並且我也無法顯示其他控件,除非佈局設置爲垂直,如@ kabuko的答案中所述 – 2012-03-22 17:46:45