Android 在for循环中添加TextView

Android 在for循环中添加TextView,android,android-layout,Android,Android Layout,假设有三个引号,那么我想有三个文本视图。但是,上面的代码使我的应用程序崩溃。有明显的错误吗?下面是我得到的错误: scrollview = (ScrollView)findViewById(R.id.detailedScrollView); for (Quotation quotation : object.quotes){ TextView quote = new TextView(this); quote.setText(quot

假设有三个引号,那么我想有三个文本视图。但是,上面的代码使我的应用程序崩溃。有明显的错误吗?下面是我得到的错误:

      scrollview = (ScrollView)findViewById(R.id.detailedScrollView);


  for (Quotation quotation : object.quotes){

          TextView quote = new TextView(this);
          quote.setText(quotation.getQuote());
          quote.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
          scrollview.addView(quote);


      }

不能直接在scrollview中添加视图。scrollview只能包含一个布局对象。您需要做的是在scrollview中添加一个linearlayout,然后将textview添加到linearlayout的布局容器中,该容器用于用户可以滚动的视图层次结构,允许其比物理显示更大。ScrollView是一个框架布局,这意味着您应该在其中放置一个子元素,其中包含要滚动的全部内容;此子对象本身可能是具有复杂对象层次结构的布局管理器。通常使用的子项是垂直方向的线性布局,表示用户可以滚动的顶级项的垂直数组

TextView类还负责自己的滚动,因此不需要ScrollView,但将两者结合使用可以在更大的容器中实现文本视图的效果

致以最良好的祝愿, Psycho

您需要在ScrollView中添加一个“LinearLayout”(或“RelativeLayout”)。 假设您的布局xml如下所示:

11-06 17:35:53.214: E/AndroidRuntime(1430): java.lang.IllegalStateException: ScrollView can host only one direct child
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <LinearLayout
    android:id="@+id/linearlayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"        
    >       
  </LinearLayout>
</ScrollView>

航海日志怎么说?您遇到了什么错误?11-06 17:35:53.214:E/AndroidRuntime(1430):java.lang.IllegalStateException:ScrollView只能承载一个直接子对象啊ScrollView只能有一个子对象?我想我需要把所有东西都放在一个线性布局中,然后放到滚动视图中?
LinearLayout linearLayout =(LinearLayout) this.findViewById(R.id.linearlayout1);
for (Quotation quotation : object.quotes){
     TextView quote = new TextView(this);
     quote.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     quote.setPadding(4, 0, 4, 0);    //left,top,right,bottom
     quote.setText(quotation.getQuote());          
     linearLayout.addView(quote);
}