我目前正在尝试使用Android在基于旧派战舰棋盘游戏的基础上开发一款游戏。目前,我只是在四处弄乱,试图对它以及制作游戏可能需要的组件有所了解。

基本上,我现在在布局xml文件中具有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<org.greene.battleship.BoardView
    android:id="@+id/board_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true" />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <org.greene.battleship.ShipView
            android:id="@+id/ships_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true" />
    </RelativeLayout>
</RelativeLayout>

我希望BoardView仅占据屏幕的特定部分,即 View 应该是我要在该 View 中创建的内容的大小。在这种情况下是2D板。这是通过从扩展View重写onMeasure()方法来完成的。 View 的宽度保持不变,但高度的值与提供完美正方形的宽度的值相同。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){

    int parent_width = MeasureSpec.getSize(widthMeasureSpec);
    this.setMeasuredDimension(parent_width, parent_width);
    Log.d("BOARD_VIEW", "BoardView.onMeasure : width = " + this.getMeasuredWidth() + ", height = "
            + this.getMeasuredHeight());
}

我通过重写 View onSizeChanged()函数并检查其中的值来查看 View 尺寸是否已更改。
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
    Log.d("BOARD_VIEW", "BoardView.onSizeChanged : width = " + w + ", height = " + h);
    board = new Board(w, h, game_activity);
    super.onSizeChanged(w, h, oldw, oldh);

}

从布局文件可以看出,然后我有了一个RelativeLayout View 组,该组将另一个名为ShipView的自定义 View 作为子级保存。理想情况下,我想发生的事情是当我测量其尺寸时,其尺寸被限制在onMeasure中设置的范围内。我以类似的方式通过它的onMeasure()方法检查ShipView的尺寸。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){

    int parent_width = MeasureSpec.getSize(widthMeasureSpec);
    int parent_height = MeasureSpec.getSize(heightMeasureSpec);

    Log.d("SHIP_VIEW", "ShipView.onMeasure : width = " + parent_width + ", height = " + parent_height);

    this.setMeasuredDimension(parent_width, parent_height);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

日志文件向我显示以下内容(onMeasure()方法似乎多次调用,但是所有值都相同,因此我不会费心显示多个日志,因为它们的值都相同):
05-04 16:36:19.428: DEBUG/BOARD_VIEW(405): BoardView.onMeasure : width = 320, height = 320
05-04 16:36:19.939: DEBUG/BOARD_VIEW(405): BoardView.onSizeChanged : width = 320, height = 320
05-04 16:36:20.429: DEBUG/SHIP_VIEW(405): ShipView.onMeasure : width = 320, height = 430

看来,当我通过ShipViews onMeasure()获得尺寸时,什么都没有改变,并且忽略了我设置的尺寸限制。我不确定是否与ShipView的RelativeLayout有关。因为它们已经更改,我是否必须为此设置LayoutParams?我认为,如果您更改父级的 View 尺寸,它将被传递给子级。

对于这样的游戏,这样做是否是正确的方法肯定值得讨论,但是无论哪种方式,我都想知道如何完成(我想它可以..?)。任何帮助将非常感激。

最佳答案

归功于question:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // let the super class handle calculations
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    // set again the dimensions, this time calculated as we want
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth() / 2);
    // this is required because the children keep the super class calculated dimensions (which will not work with the new MyFrameLayout sizes)
    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View v = getChildAt(i);
        // this works because you set the dimensions of the ImageView to FILL_PARENT
        v.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(),
                MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                getMeasuredHeight(), MeasureSpec.EXACTLY));
    }
}

10-08 09:23