我正在使用API 21.在布局文件中,我有父亲RelativeLayout,它有3个孩子:1 RelativeLayout,2 GridView。这是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeGameLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false" >
<RelativeLayout
android:id="@+id/infoLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android:id="@+id/hintTitle_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/hintsCount_tv"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:paddingBottom="0dp"
android:paddingTop="0dp"
android:text="@string/hints"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/hintsCount_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="false"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_alignParentStart="false"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/hintTitle_tv"
android:layout_toRightOf="@+id/hintTitle_tv"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/roughCounts"
android:textSize="20sp" />
<TextView
android:id="@+id/timerTitle_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/hintTitle_tv"
android:layout_marginLeft="85dp"
android:layout_marginStart="85dp"
android:layout_toEndOf="@id/hintsCount_tv"
android:layout_toRightOf="@+id/hintsCount_tv"
android:text="@string/timer"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/timerCount_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/hintsCount_tv"
android:layout_alignBottom="@+id/hintsCount_tv"
android:layout_toEndOf="@id/timerTitle_tv"
android:layout_toRightOf="@+id/timerTitle_tv"
android:text="@string/roughTimer"
android:textSize="20sp" />
<TextView
android:id="@+id/clicksTitle_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/hintTitle_tv"
android:layout_marginLeft="85dp"
android:layout_marginStart="85dp"
android:layout_toEndOf="@id/timerCount_tv"
android:layout_toRightOf="@+id/timerCount_tv"
android:text="@string/numClicks"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/clicksCount_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/hintsCount_tv"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_toEndOf="@id/clicksTitle_tv"
android:layout_toRightOf="@id/clicksTitle_tv"
android:text="@string/roughCounts"
android:textSize="20sp" />
</RelativeLayout>
<GridView
android:id="@+id/targets_gridView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentEnd="false"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="false"
android:layout_below="@id/infoLay"
android:layout_centerVertical="false"
android:columnWidth="25dp"
android:horizontalSpacing="5dp"
android:numColumns="10"
android:verticalSpacing="10dp" >
</GridView>
<GridView
android:id="@+id/game_gridView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="false"
android:layout_below="@id/targets_gridView"
android:layout_marginBottom="14dp"
android:layout_marginTop="14dp"
android:columnWidth="25dp"
android:contentDescription="@string/gameGrid"
android:horizontalSpacing="5dp"
android:numColumns="10"
android:scrollbars="none"
android:stretchMode="none"
android:verticalSpacing="5dp"
android:background="@color/background_floating_material_light" >
</GridView>
我想在这里做的是,game_gridView
我想找到它的大小,所以我可以知道将添加多少行,并根据我将图像添加到数组&amp;将它传递给适配器。但在这里,我得到身高和高度宽度仅为0。经过大量的研究后,我尝试了各种方法来实现它,但都失败了。
private void initGameObjectsGrid() {
int OBJECT_SIZE = 20;
int ROW_SIZE = OBJECT_SIZE + 5;
int COLUMN_SIZE = OBJECT_SIZE + 5;
int more_height_margin, more_width_margin;
int totalObjs;
Integer[] mThumbIds = {
R.drawable.droid_1, R.drawable.droid_3,
R.drawable.droid_2, R.drawable.ic_launcher
};
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeGameLay);
ViewGroup.LayoutParams layParams = rl.getLayoutParams();
gameView = (GridView) this.findViewById(R.id.game_gridView);
Log.d("GA GD", " LayParams G_Height : " + layParams.height + " G_Width : " + layParams.width);
// Find height & Width of the gameView
//gameView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
gameView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
int width = gameView.getWidth();
int height = gameView.getHeight();
gameHeight = height;
gameWidth = width;
Log.d("GA GD", "Height : " + height + " Width : " + width + "G_Height : " + gameHeight + " G_Width : " + gameWidth );
// REmove the Listener
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
gameView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
gameView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
//gameHeight = gameView.getMeasuredHeight(); //gameView.getHeight();
//gameWidth = gameView.getMeasuredWidth(); //gameView.getWidth();
//COLUMN_SIZE = gameView.getColumnWidth();
// Find how many rows & cols will be in the view
gameRowsCount = (int)gameHeight / ROW_SIZE;
gameColsCount = (int)gameWidth / COLUMN_SIZE;
Log.d("GA GD", "Height : " + gameHeight + " Width : " + gameWidth + " Rows : " + gameRowsCount + " Cols : " + gameColsCount);
// Total Objects to be shown in the game view
totalObjs = (int) (gameColsCount * gameRowsCount);
// Find more/extra (if) space is left in gameView after placing rows & cols
more_height_margin = (int) (gameHeight - (ROW_SIZE * gameRowsCount));
more_width_margin = (int) (gameWidth - (COLUMN_SIZE * gameColsCount));
int sideMargin = (int) (more_width_margin / 2);
int topMargin = (int) (more_height_margin / 2);
Log.d("GA GD", "MArgins Add HGT : " + more_height_margin + " WDT : " + more_width_margin + " SIDE : " + sideMargin + " TOP : " + topMargin);
if (gameView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) gameView.getLayoutParams();
p.setMargins(sideMargin, topMargin, sideMargin, topMargin);
//gameView.requestLayout();
}
Log.d("GA GD", "Total Objects : " + totalObjs + " Rows : " + gameRowsCount + " Cols : " + gameColsCount );
gameObjsArray = new ArrayList<ImageObject>();
在gameObjsArray
,
gameObjAdapter = new GameObjectsAdapter(this, R.id.game_gridView, gameObjsArray);
gameView.setAdapter(gameObjAdapter);
获得高度和宽度的其他原因是使用以边距形式留下的空白区域。
TextView
中的最后一个infoLay
朝着正确的方向前进在模拟器中不可见,但在我的设备中它可以正常显示。请记住,我的屏幕仅处于水平方向。
你能帮我解决这个问题吗?我被困在这里&amp;从小时起试图解决,但无法完成。行gameView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
似乎不是仅执行,因为登录只是不可见。在所有日志中,Height = 0&amp;宽度= 0。
请帮助我卡住&amp;匆忙也很快就解决了。
非常感谢任何帮助。
由于