2014-05-24 2 views
0

동적으로 내 앱용 버튼을 만들고이를 스크린에 무작위로 배치하려고합니다.어떻게 동적으로 생성 된 버튼을 임의로 배치 할 수 있습니까?

버튼 만들기 내부 onCreate입니다 : 내가 선형 레이아웃 방법 setMargins 사용하여 위치를 시도하고

// Create buttons 
    for (int i = 0; i < NUMBER_OF_BUTTONS; i++) { 
     Button hiddenButton = new Button(this); 
     // Set new button properties 
     hiddenButton.setId(i); 
     hiddenButton.setOnClickListener(setOnClick(hiddenButton)); 
     // Add the button to the array of buttons 
     buttonsArr[i] = hiddenButton; 

    } 

을 그리고이 기능을 가지고하는 것은 무작위로 배치하는 :

private void placeButtonsOnScreen() { 

    Random r = new Random(); 

    LinearLayout layout = (LinearLayout) findViewById(R.id.ordered_buttons_layout); 

    for (int i = 0; i < NUMBER_OF_BUTTONS; i++) { 
     final Button hiddenButton = buttonsArr[i]; 
     // Get random horizontal margin for the button 
     LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT); 
     horizontalMargin = r.nextInt(Math.abs(SCREEN_WIDTH 
       - hiddenButton.getWidth())); 
     layoutParams.setMargins(horizontalMargin, 50, 0, 0); 
     // Add the button to the screen 
     layout.addView(hiddenButton, layoutParams); 
     Log.d("button width", String.valueOf(hiddenButton.getWidth())); 
    } 
} 

문제를 getWidth() 함수는 0을 반환합니다. 화면 양쪽에 잘리지 않고 (즉, 버튼의 너비를 뺀) 화면에 버튼을 배치하려면 어떻게해야합니까?

+0

때 placeButtonsOnScreen()를 호출합니까? – pskink

답변

0

할 수 있습니다 만, 레이아웃 패스 후 뷰 치수를 얻을, 그래서 당신은 placeButtons를 호출 할 경우 대신 전화 :

getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

     @Override 
     public void onGlobalLayout() { 
      placeButtons(); 
      getViewTreeObserver().removeOnGlobalLayoutListener(this); 
     } 
    }); 

업데이트 :

int btnCount = 10; 
    final LinearLayout layout = (LinearLayout) findViewById(R.id.action_bar); 
    for (int i = 0; i < btnCount; i++) { 
     layout.addView(new Button(this),new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
    } 
    layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      layout.getViewTreeObserver().removeOnGlobalLayoutListener(this); 

      for(int i = 0; i<layout.getChildCount(); i++) { 
       View view = layout.getChildAt(i); 
       if(view instanceof Button) { 
        LinearLayout.LayoutParams layoutParams = (LayoutParams) view.getLayoutParams(); 
        int horizontalMargin = r.nextInt(Math.abs(SCREEN_WIDTH - view.getWidth())); 
        layoutParams.setMargins(horizontalMargin, 50, 0, 0); 
        view.setLayoutParams(layoutParams); 
       } 
      } 
     } 
    }); 
    layout.requestLayout(); 
+0

getViewTreeObserver 란 무엇입니까? 해당 기능을 인식하지 못합니다. – itaied

+0

보기에서 호출되었습니다. yourMainView.getViewTreeObserver() ... – ElDuderino

+0

'최종 LinearLayout 레이아웃 = (LinearLayout) findViewById (R.id.ordered_buttons_layout); \t \t layout.getViewTreeObserver(). addOnGlobalLayoutListener ( \t \t \t \t 새로운 OnGlobalLayoutListener() { \t \t \t \t \t @SuppressLint ("NewApi") \t \t \t \t \t @Override \t \t \t \t \t 공개 void onGlobalLayout() { \t \t \t \t \t \t placeButtonsOnScreen(); \t \t \t \t \t \t layout.getViewTreeObserver() \t \t \t \t \t \t \t \t .removeOnGlobalLayoutListener (이); \t \t \t \t \t} \t \t \t \t은}); 그러나'로그에 난 여전히 그것을 당신은 레이아웃에 버튼을 추가 할 필요가 hiddenButton.width'의 크기가()'0 – itaied

관련 문제