2012-03-22 100 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