如何在Android应用程序中使用RelativeLayout将图像按钮移动到特定位置?

发布于 2024-12-24 19:09:08 字数 996 浏览 4 评论 0原文

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

I am creating an Android application that moves an image button randomly on screen. I am confused which layout can help. The absolute layout used to have x and y axis but it's now depreceated. I have tried using relative, but its not working, too. I want to generate random nos to put that into x, y position so that my button keeps on moving.

Here is what I did:

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

}

Here machar is the image button. This code is not working and the XML file contains a relative layout tag named l1 and inside it an image button named machar. What I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩心不撩汉 2024-12-31 19:09:09

正如您所发现的,相对布局中没有绝对位置。您可以随机更改 machar 的 margin right 和 margin top 来移动您的 ImageButton。

As you have discovered there is no absolute position in relative layout. You could change you margin right and margin top of machar by random to move your ImageButton.

木槿暧夏七纪年 2024-12-31 19:09:09

我认为您的问题可能是您正在尝试添加已添加的视图。 ImageButton machar 已在 xml 中声明,因此当您调用 setContentView 时会在屏幕上绘制。

我建议以编程方式创建 ImageButton machar - 也就是说,不在 xml 布局中声明它,而是在 onCreate 方法中创建它。

I think your problem might be that you are trying to add a view that has already been added. The ImageButton machar has already been declared in xml and therefore drawn on the screen when you called setContentView.

What i suggest is creating the ImageButton machar programmatically - that is, not declaring it in your xml layout, but instead creating it during your onCreate method.

滿滿的愛 2024-12-31 19:09:09

修改一些边距后,您需要调用 requestLayout()

public void requestLayout()

当某些内容发生变化导致该视图的布局无效时调用此函数。这将安排视图树的布局过程。

您必须在 View 元素上调用 requestLayout() ,如果该元素上没有 set ,则边距将控制 RelativeRule 内的位置。代码>相对布局。

After modify some margin you need to make a call to requestLayout()

public void requestLayout()

Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.

You must call requestLayout() on your View element, and if no RelativeRule are set over the element, margin will control the position inside the RelativeLayout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文