简体   繁体   English

Android:调整自定义视图的大小。 父级已调整大小,但宽度/高度更改未传递给子视图

[英]Android: Resizing custom views. Parent is resized but width/height changes not passed down to child view

I am currently trying to make a game based on the old school battleship board game using android.我目前正在尝试使用 android 制作基于老式战舰棋盘游戏的游戏。 I am kinda just messing around at the moment trying to get a feel for it and for the components that I may need to make the game.我现在有点乱七八糟,试图感受一下它和我制作游戏可能需要的组件。

Basically what I have at the moment in my layout xml file is the following:基本上我目前在我的布局 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>

I want the BoardView to only take up a certain portion of the screen ie the view should be size of the content that I want to create in this view.我希望 BoardView 只占用屏幕的某个部分,即视图应该是我要在此视图中创建的内容的大小。 Which in this case is a 2D board.在这种情况下是 2D 板。 This is done by overriding the onMeasure() method from extending View.这是通过覆盖扩展 View 的 onMeasure() 方法来完成的。 The view's width is kept the same but the height is given the same value as the width giving a perfect square.视图的宽度保持不变,但高度的值与给出完美正方形的宽度相同。

@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());
}

I check to see if the views dimensions have changed by overriding the views onSizeChanged() function and checking the values there.我通过覆盖视图 onSizeChanged() function 并检查那里的值来检查视图尺寸是否已更改。

@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);

}

As can be seen from the layout file, I then have a RelativeLayout view group that holds another custom view called ShipView as a child.从布局文件中可以看出,我有一个 RelativeLayout 视图组,其中包含另一个名为 ShipView 的自定义视图作为子视图。 And ideally what I want to have happen is when I go to measure its dimensions, its dimensions have been confined to what has been set in onMeasure.理想情况下,我想要发生的是当我用 go 来测量它的尺寸时,它的尺寸已经被限制在 onMeasure 中设置的范围内。 I check the dimensions of the ShipView via its onMeasure() method in a similar way.我以类似的方式通过其 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);
}

The log files show me the following (the onMeasure() method seems to get called more than once but all the values are the same so I wont bother showing the multiple logs as the values are all the same):日志文件向我显示以下内容(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

It seems that when I get the dimensions via the ShipViews onMeasure() nothing has changed and ignores the dimension restrictions I have set.似乎当我通过 ShipViews onMeasure() 获取尺寸时,没有任何改变,并且忽略了我设置的尺寸限制。 I am not sure whether it has something to do with the RelativeLayout of the ShipView.我不确定它是否与 ShipView 的 RelativeLayout 有关。 Do I have to set the LayoutParams for that since they have changed?既然它们已经改变,我是否必须为此设置 LayoutParams? I thought that if you change the views dimension of the parent it would have been passed down to the children.我认为如果您更改父级的视图维度,它将被传递给子级。

Whether this is the right way to go about doing this for a game of this sort is definitely up for discussion but either way I would like to know how it can be done (I presume it can..?).这是否是 go 为此类游戏执行此操作的正确方法肯定有待讨论,但无论哪种方式我都想知道它是如何完成的(我认为它可以......?)。 Any help would be much appreciated.任何帮助将非常感激。

Credit to this 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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:在自定义视图中获取父级布局宽度以设置子级宽度 - Android: get parent layout width in custom view to set child width 如何在绘制之前获取调整大小的自定义视图的宽度和高度 - How to get width and height of resized custom view , before it is drawn 根据父级宽度和高度调整子视图的大小 - Resize child view based on parent width and height Android-片段/自定义视图和布局宽度/高度 - Android - fragments/custom views and layout width/height 重置父视图高度时,ConstraintLayout 子视图的高度保持为 0 - ConstraintLayout child views retain height of 0 when parent view height is reset 如何在调整其父 ViewGroup 后调整子视图高度? - How to adjust child view height after resizing its parent ViewGroup? 如何使用宽度和高度的百分比将子视图添加到父RelativeLayout - How to add child Views to parent RelativeLayout with percentage of width and height 自定义视图的自定义宽度和高度-Android - Custom width and height of a custom view-Android 为自定义视图设置Android的画布宽度和高度 - Set Canvas Width and Height Android for Custom View 自定义视图中子视图的 ID 在 Android 中是相同的 - ID of the child views in a custom view are same in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM