簡體   English   中英

如何以編程方式添加多個LinearLayout,與現有的相同

[英]How To add Multiple LinearLayout Programatically same as existing one

Xml快照: 在此處輸入圖片說明

我只是想根據用戶要求使用for循環將相同的子LinearLineLayout多次添加到Parent LinearLayout中,並使用與子內容相同的內容(我向子LinearLayout中添加了一些TextView和ImageView);

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 
    }
}

}

您可以將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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM