0

onMeasure の何が問題なのか理解するのに苦労しています。次のレイアウトの 2x2 グリッドが必要ですが、幅と高さを確認すると、高さは期待どおりに計算されていますが、幅は Integer.MAX_VALUE です。幅に起こっていない高さに何が起こっているのかわかりません。予想される 2x2 グリッドを取得するための正しい onMeasure() メソッドを理解するのに役立つヘルプをいただければ幸いです。例は 2x2 グリッドですが、どのサイズのグリッドでも機能するソリューションが必要です。

以下は XML です。DragLayer は FrameLayout を拡張するだけであることに注意してください。カスタムタグがフォーマットの違いを引き起こすとは思いませんが、念のため残しました.

<?xml version="1.0" encoding="utf-8"?>
<com.hogenson.dragndrop.DragLayer 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.hogenson.dragndrop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    custom:sfen="@string/start_sfen" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.hogenson.dragndrop.DragView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                custom:border_color="#000000"
                custom:anti_alias="true"
                custom:game_token="r" />

            <com.hogenson.dragndrop.DragView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                custom:border_color="#000000"
                custom:anti_alias="true"
                custom:game_token="p" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.hogenson.dragndrop.DragView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                custom:border_color="#000000"
                custom:anti_alias="true"
                custom:game_token="K" />

            <com.hogenson.dragndrop.DragView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                custom:border_color="#000000"
                custom:anti_alias="true"
                custom:game_token="R" />

        </TableRow>

    </TableLayout>

</com.hogenson.dragndrop.DragLayer>

そして、これが DragView クラスに実装しようとしている onMeasure() メソッドです。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode != MeasureSpec.EXACTLY)
    {
        width = Integer.MAX_VALUE;
    }

    if (heightMode != MeasureSpec.EXACTLY)
    {
        height = Integer.MAX_VALUE;
    }

    this.setMeasuredDimension(width, height);
}

編集:

Eric が以下に提供するソリューションは、水平方向のレイアウトにも layout_weight を追加する必要があることを追加して、私にとっては完璧に機能しました。

4

1 に答える 1

1

ニトピック

if両方のステートメントで、あなたが変更しているという事実widthでしょうか?

if (widthMode != MeasureSpec.EXACTLY)
{
    width = Integer.MAX_VALUE;
}

if (heightMode != MeasureSpec.EXACTLY)
{
    width = Integer.MAX_VALUE; // Shouldn't this be height?
}

それはさておき…

私の好みの解決策にたどり着く前に、まずandroid:stretchColumns="*"TableLayout. これにより、列が割り当てられたスペースを埋めることができます (小さすぎません)。

代わりに LinearLayouts を使用する

ただし、達成したいことは s で行うのが最善だと思いますLinearLayout。基本的に、これは:

<LinearLayout
    ...
    android:orientation="vertical" >

    <LinearLayout
        ...
        android:orientation="horizontal" >

        <com.hogenson.dragndrop.DragView
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <!-- more cells for row 1 -->

    </LinearLayout>

    <LinearLayout
        ...
        android:orientation="horizontal" >

        <com.hogenson.dragndrop.DragView
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <!-- more cells for row 2 -->

    </LinearLayout>

</LinearLayout>

最後に四角

最後に、オブジェクトを正方形 (同じ幅と高さ) にしたい場合は、この回答をご覧ください。

onMeasure上記の解決策では、この場合はおそらくいじらない方がよいことに注意してください。XML がそれを処理できるはずです。そしていつものように、頑張ってください!

于 2012-10-03T02:13:19.777 に答える