如何在循环中的linearlayout添加多个textview

时间:2013-04-01 05:24:57

标签: android android-layout

我想用循环添加一些textview到线性布局但是我想我只是添加一个因为我的文本视图的文本没有垂直显示。

LinearLayout llPrincipal = (LinearLayout)findViewById(R.id.mainLayout);

int contador = 0;
while ((inputString = inputReader.readLine()) != null) {
    contador++;
    stringBuffer.append(inputString + "\n");
    TextView tvRutina = new TextView(this);
    tvRutina.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    tvRutina.setPadding(0, 3, 0, 3);
    tvRutina.setId(contador);
    tvRutina.setText(inputString);

    llPrincipal.addView(tvRutina);
}

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

2 个答案:

答案 0 :(得分:1)

只需在你的循环中添加以下代码: -

LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.VERTICALLY);
llPrincipal .addView(A);

while ((inputString = inputReader.readLine()) != null) {
    LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.VERTICALLY);
contador++;
stringBuffer.append(inputString + "\n");
TextView tvRutina = new TextView(this);
tvRutina.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tvRutina.setPadding(0, 3, 0, 3);
tvRutina.setId(contador);
tvRutina.setText(inputString);

A.addView(tvRutina);
llPrincipal .addView(A);
}

答案 1 :(得分:0)

LinearLayout MainLL= (LinearLayout) findViewById(R.id.myLayoutId); 
for(int i=0; i<5; i++){
 TextView text = new TextView(this);
 text.setText("The Value of i is :"+i); // <-- does it really compile without the + sign?
 text.setTextSize(12);  
 text.setGravity(Gravity.LEFT);
 text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,          

 LayoutParams.WRAP_CONTENT));
 MainLL.addView(text);
}