画面上で画像ボタンをランダムに移動する Android アプリケーションを作成しています。どのレイアウトが役立つか混乱しています。絶対レイアウトには x 軸と y 軸がありましたが、現在は廃止されています。私はrelativeを使ってみましたが、うまくいきません。ボタンが動き続けるように、ランダムな番号を生成してx、yの位置に配置したいと考えています。
これが私がしたことです:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
submit = (Button)findViewById(R.id.submit1);
submit.setOnClickListener(this);
machar = (ImageButton)findViewById(R.id.machar);
ed=(EditText)findViewById(R.id.eT1);
tb=(ToggleButton)findViewById(R.id.tb1);
tb.setOnClickListener(this);
RelativeLayout r1 =(RelativeLayout)findViewById(R.id.l1);
LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.leftMargin=10;
params.rightMargin=20;
params.topMargin=30;
params.bottomMargin=40;
r1.setLayoutParams(params);
r1.addView(machar);
}
machar
画像ボタンはこちら。このコードは機能せず、XML ファイルには l1 という名前の相対レイアウト タグと、その中に machar という名前の画像ボタンが含まれています。私が間違っていることは何ですか?