2012-04-15 2 views
2

Я хочу отобразить x номер xml-макета внутри действия, основанного на цикле данных из массива. Для этого примера, я изменил размер массива с фиксированным значением 3.Динамический макет инфлятора в петле

XML-макета я желающим раздувать это:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 
<TextView 
    android:id="@+id/tv_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>  
    <RadioGroup android:id="@+id/rg_type" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
    <RadioButton 
     android:id="@+id/radioButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Yes" 
     android:tag="1" /> 
    <RadioButton 
     android:id="@+id/radioButton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="No" 
     android:tag="0" /> 
    <RadioButton 
     android:id="@+id/radioButton3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Don't Know" 
     android:tag="3" />  
</RadioGroup> 
</LinearLayout> 

XML-родительской страницы:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 
<LinearLayout 
    android:id="@+id/ll_content" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
</LinearLayout> 
</LinearLayout> 

Это код деятельности:

public class PainRecord_FollowUp extends Activity { 

LinearLayout llContent; 
Context context; 
PainDataItem pd_in; //this is the Parcelable extra received 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.painrecord_followup);  

    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     pd_in = extras.getParcelable("m_PainDataItem"); 
    }   
    LayoutInflater layoutInflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll_content); 
    List views = new ArrayList(); 

    for(int i=0; i<3; i++){ 
     View view = layoutInflator.inflate(R.layout.row_painrecord_followup, null); 
     TextView textView = (TextView) view.findViewById(R.id.tv_title); 
     textView.setText("did x help " + i); 
     view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     views.add(view); 
    } 
    for(int i = 0; i<views.size(); i++) 
     insertPoint.addView((View) views.get(i)); 
} 
} 

Вместо того чтобы показывать каждую раскладку под е ACH другой стороны, он отображает это следующим образом:

enter image description here

Если изменить к LayoutParams.FILL_PARENT, то только [0] значение цикла шоу.

Любые советы очень получены.

Благодаря

+0

Я не уверен, что я понимаю, вы хотите, радио-кнопки появляются вертикально, а не горизонтально? – Chironex

+0

Нет, я хочу, чтобы они были горизонтальными; каждый макет имеет 3 переключателя. Проблема в том, что каждый макет отображается друг на друге. – DaveSav

ответ

2

Установите ориентацию LinearLayout вертикальному

<LinearLayout 
    android:id="@+id/ll_content" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
</LinearLayout> 
+0

В дополнение вы можете удалить строку 'view.setLayoutParams (новые LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); '. Макет задается с помощью вашего определения XML, поэтому вам не нужно его программировать – 207

+0

Bah! Как я мог быть настолько stoopid !! Да, добавьте ориентацию и измените один из LayoutParams на FILL_PARENT. Огромное спасибо :) – DaveSav

Смежные вопросы