[英]Putting LinearLayout to LinearLayout Array
我想創建按鈕1到9,我想循環執行。 但是在每3個計數中,我想創建一個新的LinearLayout。
final LinearLayout[] ll2 = new LinearLayout[10]; // create an empty array;
for(int i=1; i<=9;i++)
{
Button btnNums = new Button(this);
final LinearLayout[] ll2 = new LinearLayout[10]; // create an empty array;
for(int i=1; i<=9;i++)
{
Button btnNums = new Button(this);
btnNums.setText(i+"");
ll.addView(btnNums);
if(i%3==0){
ll2[i] = ll;
ll = null;
}
}
layout.addView(ll2[0]);
btnNums.setText(i+"");
ll.addView(btnNums);
if(i%3==0){
ll2[i] = ll;
ll = null;
}
}
layout.addView(ll2[0]);
這是行不通的。 我沒有收到任何錯誤,但是當o運行該應用程序時,它停止工作。 有什么問題?
我在項目中使用了它,並為我工作,就像這樣。 希望能幫助到你
在類級別聲明空數組:
LinearLayout[] imageLayoutContainers = new LinearLayout[10];
然后在onCreate methord上:
for (int i = 0; i < imageLayoutContainers.length; i++) {
imageLayoutContainers[i] = new LinearLayout(this);
imageLayoutContainers[i].setOrientation(LinearLayout.VERTICAL);
imageLayoutContainers[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageLayoutContainers[i].setBackgroundResource(imagesIds[i]);
}
很好,謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.