[JAVA] [Android] Erstellen Sie eine quadratische Ansicht, während Sie das Seitenverhältnis beibehalten: SquareLayout

Erstellen wir eine benutzerdefinierte Ansicht mit dem Namen 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);
    }
}

Verwenden Sie es im Layout wie folgt.

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

Ich konnte vier quadratische Knöpfe so anordnen.

Das Original war stackoverflow here.

Recommended Posts

[Android] Erstellen Sie eine quadratische Ansicht, während Sie das Seitenverhältnis beibehalten: SquareLayout
[Kotlin / Android] Erstellen Sie eine benutzerdefinierte Ansicht
Ein Neuling versucht, die Android-Ansicht zusammenzufassen (Entwicklung von Android-Anfängern)
[Android] Erstellen Sie einen Kalender mit GridView