2

我有一个关于 imagebutton 数组的问题。我想通过更改RelativeLayout 中的marginTop 和marginLeft 以编程方式将数组的每个图像按钮放置在不同的位置。这是我的代码,但它只显示同一位置的所有图像按钮,就像它们没有边距一样:

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]);

    }
}
4

1 回答 1

2

当您FrameLayout.LayoutParams的布局是RelativeLayout. 如果我没有完全弄错,你应该RelativeLayout.LayoutParams改用。

于 2013-03-09T22:34:18.330 回答