如何以编程方式添加多个LinearLayout与现有的一样

时间:2017-02-16 08:49:17

标签: android android-layout

Xml的快照: enter image description here

我只想根据用户要求多次将同一个子LinearLayout添加到Parent LinearLayout中,使用 for loop ,其内容与child相同(我已将一些TextView和ImageView添加到子LinearLayout);

public class ListData extends AppCompatActivity {
    LinearLayout childLL,parentLL;
    TextView tName,tEmail,tCity;
    ImageView iImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_data);

    parentLL=(LinearLayout) findViewById(R.id.parentList);
    childLL=(LinearLayout) findViewById(R.id.childList);
    tName=(TextView) findViewById(R.id.listName);
    tEmail=(TextView) findViewById(R.id.listEmail);
    tCity=(TextView) findViewById(R.id.listCity);
    iImage=(ImageView) findViewById(R.id.listImage);

    for(int i=0;i<10;i++) {
        //parentLL.addView(childLL);
       //please Suggest code 
    }
}

}

1 个答案:

答案 0 :(得分:1)

您可以将childLayout移动到单独的布局文件并手动添加。 我们将新的布局文件称为child_layout,示例代码可以是:

LayoutInflater inflater = getLayoutInflater();
for (int i = 0, i < maxSize, i++) {
    View v = inflater.inflate(R.layout.child_layout, null);
    //customise your child view here if needed.
    parentLL.addView(v);
}