2013-03-09 26 views
2

我有一個關於imagebutton數組的問題。我想通過改變RelativeLayout中的marginTop和marginLeft以編程方式將數組中的每個圖像按鈕放置在不同的位置。這裏是我的代碼,但它只能說明在同一位置的所有imagebuttons,就像他們沒有保證金:以編程方式在視圖上放置數組的圖像按鈕

DatabaseHandler db; 
db= new DatabaseHandler(this); 
int databasereadercount=db.getMemoryCount(); 

ImageButton[] button= new ImageButton[60]; 
setContentView(R.layout.playsolo); 
RelativeLayout layout = (RelativeLayout) findViewById(R.id.playsololayout); 
for(int i=0;i<60;i++){ 
if(i<=databasereadercount){ 
    button[i]=new ImageButton(this); 
    FrameLayout.LayoutParams[] params = new FrameLayout.LayoutParams[60]; 
    params[i]=new FrameLayout.LayoutParams(100,100); 
    params[i].setMargins(10*i, 5*i, 0, 0);  //for example 
    Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.raster); 
    bitmap=Bitmap.createScaledBitmap(bitmap, 480, 320, true); 
    Drawable drawable = new BitmapDrawable(getResources(),bitmap); 
    StateListDrawable states = new StateListDrawable(); 
    states.addState(new int[] {android.R.attr.state_pressed},drawable); 
    states.addState(new int[] {android.R.attr.state_focused},drawable); 
    states.addState(new int[] { },drawable); 
    button[i].setImageDrawable(states); 
    button[i].setId(i); 
    button[i].setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View v) { 
      // TODO Auto-generated method stub 
     startlevel(v.getId()); 
     } 
    }); 
    layout.addView(button[i],params[i]); 

    } 
} 

回答

2

您使用FrameLayout.LayoutParams,而你的佈局是一個RelativeLayout。如果我沒有完全弄錯你應該使用RelativeLayout.LayoutParams

+0

好吧,我現在就來試試吧。 – user2152573 2013-03-09 22:38:30

+0

謝謝,作品! – user2152573 2013-03-09 22:45:15

+0

沒問題,不客氣。 – Griddo 2013-03-09 22:46:41

相關問題