나는 이미지를 넣기 위해 GridView
을 가지고 있습니다. 내가 무엇을하고 싶은지 getView()
방법으로 보여 질 때 이미지의 최적 크기가 무엇인지 알 수 있도록 너비와 높이와 같은 GridView
의 크기를 측정하는 것입니다. 행당 8 개의 이미지 만 표시하고 싶습니다. 그래서 장치가 더 큰 화면을 가지고 있다면 이미지에 고정 된 크기를 설정하여 이미지를 추가하는 대신 이미지의 크기가 커지게됩니다.안드로이드 뷰 getWidth 메서드는 항상 0을 반환합니다.
따라서 onCreate()
메서드에서 사용자 정의 Adapter
을 초기화하고 getWidth
및 getHeight
값을 전달합니다. 그러나 그들은 항상 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);
}
문제의 해결책을 찾으셨습니까? – JJ86