【问题标题】:Insert a TextView under an others in a RelativeLayout in a ScrollView在 ScrollView 的 RelativeLayout 中的其他人下插入 TextView
【发布时间】:2014-03-30 08:13:16
【问题描述】:

我想知道,如何在 ScrollView 中的 RelativeLayout 中添加大量 TextView(当然,textview 可以向上滚动)

在我的 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 中并用 getLastId() 粗略地说替换:@id/textView1

更粗略地说,它类似于:r.addLastView(v1)

我在等待您的回答或您的评论:)

【问题讨论】:

  • 为什么不使用 LinearLayout ?

标签: java android xml scrollview android-relativelayout


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2017-12-15
    • 1970-01-01
    相关资源
    最近更新 更多