2011-11-06 94 views
0
 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); 


     } 

假设有三个引号,那么我想要三个textView。但是,上面的代码崩溃了我的应用程序。任何明显的错误?这里是我得到的错误:在for循环中添加textViews

11-06 17:35:53.214: E/AndroidRuntime(1430): java.lang.IllegalStateException: ScrollView can host only one direct child 
+0

什么的logcat的说?你得到的错误是什么? –

+0

11-06 17:35:53.214:E/AndroidRuntime(1430):java.lang.IllegalStateException:ScrollView只能托管一个直接孩子 啊scrollview只能有一个孩子?我想我需要将所有内容放入linearLayout中,然后放入scrollview中? – Adam

回答

6

您不能直接在滚动视图内添加视图。 scrollview只能包含一个布局对象。你需要做的是在你的滚动视图中添加一个线性布局,然后将textview添加到线性布局

3

布局容器的视图层次结构可以由用户滚动,允许它比物理显示更大。 ScrollView是一个FrameLayout,这意味着你应该在其中放置一个包含整个内容滚动的子项;这个孩子本身可能是一个具有复杂对象层次结构的布局管理器。一个经常使用的孩子是一个垂直方向的LinearLayout,呈现一个顶级项目的垂直数组,用户可以滚动浏览。

TextView类还负责自己的滚动操作,因此不需要ScrollView,但使用这两者可以在较大的容器内实现文本视图的效果。 Please more detail

与问候, 心理

0

您需要添加一个 “的LinearLayout” 内滚动型(或 “RelativeLayout的”)。 假设你有布局XML如下:

<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> 

现在你要添加的“TextView的”编程,这是如下:

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); 
}