簡體   English   中英

Android:調整自定義視圖的大小。 父級已調整大小,但寬度/高度更改未傳遞給子視圖

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

我目前正在嘗試使用 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 只占用屏幕的某個部分,即視圖應該是我要在此視圖中創建的內容的大小。 在這種情況下是 2D 板。 這是通過覆蓋擴展 View 的 onMeasure() 方法來完成的。 視圖的寬度保持不變,但高度的值與給出完美正方形的寬度相同。

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

我通過覆蓋視圖 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);

}

從布局文件中可以看出,我有一個 RelativeLayout 視圖組,其中包含另一個名為 ShipView 的自定義視圖作為子視圖。 理想情況下,我想要發生的是當我用 go 來測量它的尺寸時,它的尺寸已經被限制在 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? 我認為如果您更改父級的視圖維度,它將被傳遞給子級。

這是否是 go 為此類游戲執行此操作的正確方法肯定有待討論,但無論哪種方式我都想知道它是如何完成的(我認為它可以......?)。 任何幫助將非常感激。

歸功於這個問題

@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM