我的问题是,我想改变位置,我的图像按钮是在for-loop的帮助下创建的,但是它们重叠很多如何摆脱它?
package com.example.applecounter;
import java.util.Random;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Play extends Activity implements OnClickListener {
final Context context = this;
int counter = 0;
private MediaPlayer mplayer;
private TextView txt;
int left = 0;
int top = 0;
int right= 0;
int bottom=0;
private ImageButton bt[];
/*Here i have my OnCreate function*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.RelativeLayout1);
rl.setBackgroundResource(R.drawable.background);
/*Here i have which creates my Imagebutton*/
for (int i = 0; i <2; i++) {
ImageButton btn = new ImageButton(this);
btn.setBackgroundResource(R.drawable.apple);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(params);
btn.setId(1);
params.width = 83;
params.height = 80;
//params.setMargins(10, 12, 50, 0);
btn.setLayoutParams(params);
params.setMargins(left,top,right,bottom);
rl.addView(btn);
btn.setOnClickListener(this);
/*Here i have both addview and onclicklistener*/
}
}
/*Here i have OnClick view for mhy button*/
@Override
public void onClick(View v) {
switch (v.getId()) {
// Here i have switch case//
case 1:
v.setVisibility(View.GONE);
mplayer = MediaPlayer.create(this, R.raw.sound);
//txt.setText("" + counter);
//counter++;
break;
default:
}
}
}
当我在for循环中创建imagebutton所以它的重叠太多以至于我的questin是每个按钮的随机性如何?
答案 0 :(得分:0)
你可能想尝试一下
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
btn.setLayoutParams(params);