이미 설명한대로 RelativeLayout
을 사용해야합니다. 그러나 당신의 논리는 약간 결함이 있습니다, 당신은 버튼을 화면에 표시하지 않도록 보장하기 위해 변수를 설정해야합니다. 나는이 여전히 끝낼 수있는 버튼이 중복 때문에 해결책이 아니라 이미 당신에
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
int buttonWidth = 150;
int buttonHeigh = 40;
int max_x_val = width - buttonWidth;
int max_y_val = heigth - buttonHeigh; // Edited here
for(int x = 0;x<10;x++) {
Button btn = new Button(this);
int Xpoistion = rnd.nextInt(max_x_val - buttonWidth) + buttonWidth;
int Yposition = rnd.nextInt(max_y_val - buttonHeigh) + buttonHeigh;
btn.setX(Xpoistion);
btn.setY(Yposition);
btn.setWidth(buttonWidth);
btn.setHeight(buttonHeigh);
btn.setText(x +")"+width + "," + height + " | " + Xpoistion + "," + Yposition);
relativeLayout.addView(btn);
Log.d("Debug","Xpos =" + Xpoistion);
Log.d("Debug","Ypos =" + Yposition);
}
을 코드의 비트를 추가
여전히 비트를 얻을 수 일부 버튼이 최근 편집 이것은 우리가 화면의 전체 치수를 가지고 있기 때문에 발생합니다. 우리가 필요로했던 것은 단지 Relative Layout
의 치수뿐이었습니다. 상단에있는 파란색 막대는 그것이 안되었을 때 측정에 포함되었습니다. 그러나이 오류가 불가능 더 해결하기 까다로운하지만 조금, 조금 구글과 충분한 도움을 찾을 것입니다
편집 : 내 초기 생각은 btn.setX()
및 setY()
그 픽셀에 Button
의 중심,하지만 할 것을 그것은 그 같은 픽셀 버튼의 시작을 표시, 그래서 나는 단지로 임의 화 기능을 변경했다 : 나는 다음과 같은 샘플을 쓴 중복 해결하려면
int Xposition = rnd.nextInt(width - buttonWidth);
int Yposition = rnd.nextInt(height - buttonHeight);
다음을
// Create dummy class for holding coordinates
public class ButtonCordinates{
public int x;
public int y;
public ButtonCordinates(int x, int y) {
this.x = x;
this.y = y;
}
}
//In MainActivity class create a array for holding coordinates
ButtonCoordinates [] buttonsCoordinates = new ButtonCoordinates [10];
int x = 0;
while(x < 10) {
int Xposition = rnd.nextInt(width - buttonWidth);
int Yposition = rnd.nextInt(height - buttonHeight);
ButtonCoordinates buttonCoords = new ButtonCoordinates (Xposition,Yposition);
if(!coordinatesFree(buttonCoords,buttonHeight,buttonWidth)){
// Get another chance
continue;
}
Button btn = new Button(this);
btn.setX(Xposition);
btn.setY(Yposition);
btn.setWidth(buttonWidth);
btn.setHeight(buttonHeight);
btn.setText(Xposition+";"+Yposition);
buttonsCoordinatesArray[x++] = buttonCoords;
linearLayout.addView(btn);
Log.d("Debug","Xpos =" + Xposition);
Log.d("Debug","Ypos =" + Yposition);
}
// Method that prevents overlapping
private boolean coordinatesFree(ButtonCoordinates newButton, int buttonHeight, int buttonWidth){
for(ButtonCoordinates existingButton : buttonsCoordinatesArray){
if(existingButton == null){
// First button ever
return true;
}
boolean f1 = existingButton.x + buttonWidth <= newButton.x;
boolean f2 = existingButton.y + buttonHeight <= newButton.y;
boolean f3 = existingButton.x - buttonWidth >= newButton.x;
boolean f4 = existingButton.y - buttonHeight >= newButton.y;
if(!f1 && !f2 && !f3 && !f4){
return false;
}
}
return true;
}
랜덤 값에 100을 추가하는 이유는 무엇입니까? 이렇게하면 100과 displayWidth resp 사이의 버튼의 왼쪽/위쪽 위치가 표시됩니다. 100 및 displayHeight. 그리고 거기에 배치 된 버튼은 오른쪽/아래로 화면을 벗어날 것입니다. – Ridcully
@Ridcully 방금 그걸 가지고 놀았어요, 많은 옵션을 시도했지만 아무 것도 작동하지 않습니다. – user2561521