循环内的动态布局充气机 [英] Dynamic layout inflator within a loop

查看:65
本文介绍了循环内的动态布局充气机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于可拆分数组中的数据循环,在活动中显示xml布局的 x 编号.对于此示例,我将数组大小更改为固定值3.

I want to display x number of an xml layout within an activity based upon a loop of data from a parcelable array. For this example, I've changed the array size to a fixed value of 3.

我要膨胀的布局的xml是:

The xml of the layout I am wanting to inflate is:

<?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));
}
}

与其显示每个布局彼此之间的位置,不如显示以下内容:

Instead of showing each layout underneath each other, it displays it like this:

如果我更改为LayoutParams.FILL_PARENT,则仅显示[0]循环值.

If I change to LayoutParams.FILL_PARENT, then just the [0] loop value shows.

任何收到的建议都很好.

Any advice greatly received.

谢谢

推荐答案

将LinearLayout方向设置为垂直

Set your LinearLayout orientation to vertical

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

这篇关于循环内的动态布局充气机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆