2013-03-09 61 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

相关问题