我正在创建一个在屏幕上随机移动图像按钮的 Android 应用程序。我很困惑哪种布局可以提供帮助。绝对布局曾经有 x 和 y 轴,但现在已弃用。我尝试过使用 relative,但它也不起作用。我想生成随机 nos 将其放入 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 的图像按钮。我做错了什么?