【问题标题】:How can I get a view display into random position in the screen (adaptable to different screen sizes)如何让视图显示在屏幕中的随机位置(适应不同的屏幕尺寸)
【发布时间】:2019-06-03 16:07:09
【问题描述】:

我已经有一种方法可以在屏幕的随机位置显示按钮,但它不适用于其他屏幕尺寸。我该怎么做(内部:r.nextInt())。

int buttonHeight;
int buttonWidth;
buttonHeight = button.getHeight();
buttonWidth = button.getWidth();
int xLeft = r.nextInt(480 - buttonHeight);
int yUp = r.nextInt(500 - buttonWidth);
int xRight = r.nextInt(670 + buttonHeight);
int yDown = r.nextInt(1400 + buttonWidth);

button.setX(xLeft);
button.setY(yUp);
button.setX(xRight);
button.setY(yDown);

我只是想让随机数适应每个屏幕尺寸。

【问题讨论】:

标签: java android-studio android-studio-3.0


【解决方案1】:

Android 坐标基于视图的左上角。因此,您提供给视图的 x 和 y 值实际上是视图左上角的 x 和 y。尝试将nextInt() 的参数限制为random.nextInt(maxScreenWidth - buttonWidth)random.nextInt(maxScreenHeight-buttonHeight),并将这些坐标分配给按钮的x 和y。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2020-09-20
    • 2022-10-06
    相关资源
    最近更新 更多