Инфлатор с динамично оформление в рамките на цикъл

Искам да покажа 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));
}
}

Вместо да показва всяко оформление едно под друго, то го показва така:

въведете описание на изображението тук

Ако променя на LayoutParams.FILL_PARENT, тогава се показва само стойността на цикъла [0].

Всеки съвет се приема много добре.

Благодаря


person DaveSav    schedule 15.04.2012    source източник
comment
Не съм сигурен, че разбирам, искахте ли бутоните за избор да се показват вертикално, а не хоризонтално?   -  person Chironex    schedule 15.04.2012
comment
Не, искам ги хоризонтално; всяко оформление има 3 радио бутона. Проблемът е, че всяко оформление се показва едно върху друго.   -  person DaveSav    schedule 15.04.2012


Отговори (1)


Задайте ориентацията на LinearLayout на вертикална

<LinearLayout
    android:id="@+id/ll_content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="vertical">
</LinearLayout>
person 207    schedule 15.04.2012
comment
В допълнение можете да премахнете реда view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); . Оформлението е зададено чрез вашата XML дефиниция, така че не е необходимо да го задавате програмно - person 207; 15.04.2012
comment
Бах! Как можах да бъда толкова тъп!! Да, добавете ориентацията и променете един от LayoutParams на FILL_PARENT. Много благодаря :) - person DaveSav; 15.04.2012