2013-04-01 77 views
1

我想用循環添加一些textview到線性佈局,但我想我只是添加一個,因爲我的文本視圖的文本不是垂直顯示。如何在循環中的linearlayout中添加多個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" > 

回答

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); 
} 
+0

對不起,但也不不工作也需要添加其他線性佈局 – aeRogelio

+0

@aeRogelio再次看到編輯答案 – duggu

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); 
} 
+0

沒有抱歉,我有同樣的結果 – aeRogelio

相關問題