Créons une vue personnalisée appelée SquareLayout
public class SquareLayout extends LinearLayout {
public SquareLayout(Context context) {
super(context);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
Utilisez-le dans la mise en page comme suit.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.your-package.SquareLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/something_item1" />
</com.your-package.SquareLayout>
<com.your-package.SquareLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="15dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/something_item2" />
</com.your-package.SquareLayout>
<com.your-package.SquareLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="15dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/something_item3" />
</com.your-package.SquareLayout>
<com.your-package.SquareLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="15dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/something_item4" />
</com.your-package.SquareLayout>
</LinearLayout>
J'ai pu organiser quatre boutons carrés comme celui-ci.
L'original était stackoverflow here.