2

我使用此代码获取我的屏幕宽度和高度,并将我的按钮发送到屏幕上的任意位置,但有时我的按钮完全不在屏幕上。我不相信我的代码得到的解决方案是真正的解决方案。在Android中获取屏幕宽度和高度

DisplayMetrics displayMetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
float height = displayMetrics.heightPixels; 
float width = displayMetrics.widthPixels; 

float maxX = width; 
float maxY = height; 
float minX = width/10; 
float minY = height/15; 

Random rand = new Random(); 

float finalX = rand.nextFloat() * abs(maxX - minX); 
float finalY = rand.nextFloat() * abs(maxY - minY); 

_height.setText("Current Y: " + String.valueOf(finalY)); 
_width.setText("Current X: " + String.valueOf(finalX)); 
max_height.setText("Max height: " + String.valueOf(height)); 
max_width.setText("Max width: " + String.valueOf(width)); 
btnClick.setX(finalX); 
btnClick.setY(finalY); 
btnClick.setVisibility(View.VISIBLE); 

这里是XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/cLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/background" 
app:layout_constraintRight_toRightOf="@+id/width" 
tools:context="com.jamblatech.clicker.MainActivity" 
tools:layout_editor_absoluteY="81dp"> 

<Button 
    android:id="@+id/buttonEnd" 
    android:layout_width="363dp" 
    android:layout_height="572dp" 
    android:background="@android:color/transparent" 
    android:onClick="" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="1.0" /> 

<TextView 
    android:id="@+id/textViewCount" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/zero" 
    android:textColor="@color/colorAccent" 
    android:textSize="36sp" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.03" /> 

<TextView 
    android:id="@+id/timerView" 
    android:layout_width="185dp" 
    android:layout_height="41dp" 
    android:text="@string/zero" 
    android:textAlignment="center" 
    android:textSize="24sp" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="0.502" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.937" /> 

<Button 
    android:id="@+id/buttonClick" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_marginTop="200dp" 
    android:background="@drawable/clickbutton" 
    android:text="@string/Click" 
    android:textSize="24sp" 
    android:visibility="invisible" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/textViewCount" /> 

<Button 
    android:id="@+id/buttonReplay" 
    android:layout_width="221dp" 
    android:layout_height="103dp" 
    android:text="@string/Replay" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="0.503" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 

<Button 
    android:id="@+id/buttonBack" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Back" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.674" /> 

<TextView 
    android:id="@+id/highScore" 
    android:layout_width="261dp" 
    android:layout_height="107dp" 
    android:textAlignment="center" 
    android:textColor="@android:color/holo_purple" 
    android:textSize="30sp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.149" /> 

<TextView 
    android:id="@+id/height" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Test" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.029" /> 

<TextView 
    android:id="@+id/width" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Test" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.0" /> 

<TextView 
    android:id="@+id/maxheight" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Test" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.06" /> 

<TextView 
    android:id="@+id/maxwidth" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Test" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.09" /> 

<TextView 
    android:id="@+id/testTExt" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Test" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.0" /> 

<TextView 
    android:id="@+id/startTimerText" 
    android:layout_width="134dp" 
    android:layout_height="95dp" 
    android:text="@string/Test_number" 
    android:textAlignment="center" 
    android:textColor="@android:color/holo_green_dark" 
    android:textSize="60sp" 
    android:visibility="visible" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.54" /> 
    </android.support.constraint.ConstraintLayout> 

这里有两张照片显示我的问题:

  1. 按钮略微屏幕上:
    Button slightly on-screen
  2. 按钮完全关闭屏幕: Button completely off-screen
+1

你能分享你的xml文件吗? – Ashwani

+0

我添加了XML :) –

+0

尝试用RelativeLayout替换ConstraintLayout –

回答

0

你可以做的是: 你已经随机产生的X和Y, x + button.getWidth()它并检查它是否小于screen width如果是你的x是很好的约束,如果没有regerenate您的随机X和Y,相似的,如果x在绑定做它为y并检查y + button.getHeight()是否小于screen height它会这样做。