gpt4 book ai didi

java - 设置屏幕属性 View

转载 作者:行者123 更新时间:2023-11-30 11:07:23 24 4
gpt4 key购买 nike

在下面的代码中,按钮会在单击时更改其位置。但有时,按钮会移出屏幕,导致无法再点击。如何解决?

@Override

public void onClick(View v) {

view.getParent();
Random r = new Random();
View decorView = getWindow().getDecorView();
int screenWidth = decorView.getWidth();
int screenHeight = decorView.getHeight();
long startTime = SystemClock.elapsedRealtime();
i++;


if (i == 1 ) {

start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 2 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}

else if (i == 3) {
long difference = SystemClock.elapsedRealtime() - startTime;

Intent intent = new Intent(Game.this, MainScreen.class);
intent.putExtra("time",difference);

textview1.setText(getIntent().getStringExtra("time"));
finish();
}


}

最佳答案

您遇到的问题似乎是您可能在 RelativeLayout 中使用 wrap_content 作为宽度/高度,但您获取了屏幕的宽度和高度,而不是包含的布局你的按钮。

您应该使用 getParent() 方法来获取您的 RelativeLayout 宽度和高度。

RelativeLayout myRelativeLayout = (RelativeLayout) myButton.getParent();
int screenWidth = myRelativeLayout.getWidth();
int screenHeight = myRelativeLayout.getHeight();

关于java - 设置屏幕属性 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28942737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com