2013-12-12 1 views
0

나는 이미지를 넣기 위해 GridView을 가지고 있습니다. 내가 무엇을하고 싶은지 getView() 방법으로 보여 질 때 이미지의 최적 크기가 무엇인지 알 수 있도록 너비와 높이와 같은 GridView의 크기를 측정하는 것입니다. 행당 8 개의 이미지 만 표시하고 싶습니다. 그래서 장치가 더 큰 화면을 가지고 있다면 이미지에 고정 된 크기를 설정하여 이미지를 추가하는 대신 이미지의 크기가 커지게됩니다.안드로이드 뷰 getWidth 메서드는 항상 0을 반환합니다.

따라서 onCreate() 메서드에서 사용자 정의 Adapter을 초기화하고 getWidthgetHeight 값을 전달합니다. 그러나 그들은 항상 0입니다.

xml 레이아웃 파일에서 gridview가 유일한보기였습니다. 그런 다음 linearlayout에 추가하여 아마도 부모의 너비와 높이를 반환하지 않았지만 여전히 0입니다. 여기

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_game); 

    if (savedInstanceState == null) { 
    } 

    int difficulty = getIntent().getExtras() 
      .getInt(AppConstants.EXTRAS_GAME_DIFFICULTY_LEVEL, 1); 

    LinearLayout lvg = (LinearLayout) findViewById(R.id.linearForGridGame); 
    GridView gv = (GridView) findViewById(R.id.gridview); 

    gv.setAdapter(new CellAdapter(this, difficulty, lvg.getWidth(), lvg.getHeight())); 

    gv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      Toast.makeText(GameActivity.this, "" + position, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

은 XML 레이아웃 파일입니다 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearForGridGame"> 

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnWidth="128px" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="0dp" 
    android:horizontalSpacing="0dp" 
    android:stretchMode="none" 
    android:gravity="center" 
    android:choiceMode="none" 
    android:listSelector="@null" 
    android:clickable="true" /> 

    </LinearLayout> 

그리고 여기에 어댑터의 cosntructor, 난 항상 폭과 높이를 얻을 여기

는 활동의에서 onCreate 방법 0 :

public CellAdapter(Context context, int difficultyLevel, int parentWidth, int parentHeight) 
{ 
    _context = context; 
    _dificultyLevel = difficultyLevel; 
    _parentHight = parentHeight; 
    _parentWidth = parentWidth; 

    Log.d(TAG, "Received difficulty level " + _dificultyLevel); //OK 
    Log.d(TAG, "Received parent width " + _parentWidth); //Always 0 
    Log.d(TAG, "Received parent height " + _parentHight); //Always 0 

    _cellWidth = (_parentWidth/6); //Width of image to fill 6 per row 
    setupGame(_dificultyLevel); 
} 
+0

문제의 해결책을 찾으셨습니까? – JJ86

답변

1

하면 activit의 onWindowFocusChanged(boolean)lvg.getWidth()을하고 시도 y

0

보기 레이아웃이 onCreate()에서 발생하지 않았 으면 View을 서브 클래 싱하고 onLayout()이 호출 된 것을 관찰하여 직접 볼 수 있습니다.

많은 정보가 this question입니다. ViewTreeObserver.OnGlobalLayoutListener은 특정보기를 타겟팅 할 수 있으므로 onWindowFocusChanged(boolean)보다 구체적으로 표시 될 수 있습니다. this answer에서

예 : 뷰 계층이 팽창하고 치수를 알고 측정 될 때까지

yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 

     @Override 
     public void onGlobalLayout() { 
      // Ensure you call it only once : 
      yourView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 

      // Here you can get the size :) 
     } 
}); 
2

당신은 기다려야합니다. onCreate()

final ViewTreeObserver vto = lvg.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

     @Override 
     public void onGlobalLayout() { 
      if (lvg.getWidth() > 10){ // because it may be called before the view is measured and you will still get 0 
       // here you can get the measured dimensions 
       ViewTreeObserver obs = pictureImg.getViewTreeObserver(); 
       obs.removeGlobalOnLayoutListener(this); // otherwise you're gonne keep getting called 
      } 
     } 
    }); 
+0

이 일을, 고마워. –

1

에 그런 일을 추가 당신이 의 LayoutParams이 같은 사용한다고 생각 :

... 
LinearLayout lvg = (LinearLayout) findViewById(R.id.linearForGridGame); 
ViewGroup.LayoutParams layoutParams = lvg.getLayoutParams(); 
GridView gv = (GridView) findViewById(R.id.gridview); 
gv.setAdapter(new CellAdapter(this, difficulty, layoutParams.getWidth(), layoutParams.getHeight())); 
... 

android.view.ViewGroup.LayoutParams를 가져올 수 있는지 확인합니다.