我的layout.xml
文件如下所示:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="270dp"
android:layout_weight="0.47" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
我使用以下代码在循环中创建TextView
s 和Button
s :for
View linearLayout = findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);
textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
int width=90;
int height=60;
button.setLayoutParams(new LayoutParams(width, height));
((LinearLayout) linearLayout).addView(button);
}
我得到Button
下面的TextView。任何人都可以帮我替换它LinearLayout
,RelativeLayout
以便我并排得到TextView
和吗?Button