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 を追加する必要があることを追加して、私にとっては完璧に機能しました。