レイアウトで正方形のビューを強制するカスタム相対レイアウトを定義しました。しかし、その相対的なレイアウトで子供たちを中心に置くのに苦労しています。
私のカスタム RelativeLayout は次のように定義されています。
public class SquareView extends RelativeLayout {
public SquareView(Context context) {
super(context);
}
public SquareView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int size;
size = Math.min(widthSize, heightSize);
setMeasuredDimension(size, size);
int finalMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
super.onMeasure(finalMeasureSpec, finalMeasureSpec);
}
}
私のxmlは次のとおりです。
<com.mypackage.SquareView
android:id="@+id/gameboard_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:padding="0dp" >
<LinearLayout
android:id="@+id/gameboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@drawable/xmlgameboard"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</com.mypackage.SquareView>
xml ファイルをグラフィカル ビューで表示すると、SquareView (gameboard_container) が使用可能なすべてのスペース (正方形ではない) を埋めているのがわかりますが、子 LinearLayout (ゲームボード) は正方形です。
ただし、子 LinearLayout (ゲームボード) も左揃えで、中央に配置する必要があります。
どうすればこれを修正できますか?
ありがとうございました。