2014-03-30 34 views
0

我想知道,我怎么能在一个滚动型很多的TextView的数量添加到RelativeLayout的(用于TextView中可以滚动起来,当然)插入一个TextView的人下一个RelativeLayout的在滚动型

在我的XML代码,它很简单:

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/bde" > 

    <RelativeLayout 
     android:id="@+id/layout_" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

    <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:text="TextView" /> 
     <TextView 
      android:id="@+id/textView2" 
      android:layout_below="@id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TextView" /> 

    </RelativeLayout> 

</ScrollView> 

除了我想加入的Java代码,从一个DB每个TextView的(与DB没有问题),增加自己在布局和更重要的是,一个之后。

现在,我有:

//Layout in ScrollView 
RelativeLayout r = (RelativeLayout) findViewById(R.id.layout_); 

//for add TextView 
for(int i = 0 ; i < nbr_limite ; i++){ 
TextView v1 = new TextView(r.getContext()); 

v1.setText("Test n_"+i); 
v1.setId(i+1); 

r.addView(v1); 
} 

其实,我想一个:android:layout_below="@id/textView1"但在Java和更换:@id/textView1getLastId()粗暴地说。

而且更粗略地说,这将是这样的:r.addLastView(v1)

我等着你的答案或者您的评论:)

+0

为什么不使用LinearLayout? – pskink

回答

0
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

TextView tv = new TextView(r.getContext()); 
v1.setText("Test n_"+i); 
v1.setId(i+1); 

params.addRule(RelativeLayout.BELOW, tv.getId()-1); 


r.addView(tv); 

也这是你的问题的答案.... Programatically add view one below other in relative layout

相关问题