1

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

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

2 回答 2

1

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

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);
}
于 2013-04-01T05:29:51.850 回答
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);
}
于 2013-04-01T05:30:28.020 回答