我正在獲取字符串數組的動態長度,並且我想要在圖像按鈕中顯示圖像,並且也在水平視圖中添加了按鈕,但它顯示在垂直佈局中。請幫助。這是我的代碼:以編程方式動態添加imagebutton android
for (int i =0;i<adapt_objmenu.image_array.length;i++){
ImageButton b1 = new ImageButton(myrefmenu);
b1.setId(100 + i);
// b1.setText(adapt_objmenu.city_name_array[i]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
lp.addRule(RelativeLayout.BELOW, b1.getId() - 1);
}
b1.setLayoutParams(lp);
relative.addView(b1);
//relate.addView(b1, i, new RelativeLayout.LayoutParams(width,height));
//height = height+80;
}
如果我正確理解你的問題,你會意識到你正在設置RelativeLayout.BELOW,而RelativeLayout.RIGHT_OF會更有意義嗎? – 2012-04-13 11:18:24
好吧,我知道了,但如何在imagebutton上設置圖像,這是主要問題 – Ved 2012-04-13 11:21:52
您的意思是您想要在添加到'ImageButton'的RelativeLayout後設置圖像。 – Herry 2012-04-13 11:23:55