Sobes.tech
Junior

Hogyan lehet úgy elhelyezni a tárgyakat a layoutban, hogy kör alakban legyenek?

sobes.tech MI

Válasz az MI-től

Az objektumokat kör alakban elhelyezheted a ConstraintLayout és a Circular positioning segítségével.

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/center_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Központ"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gomb 1"
        app:layout_constraintCircle="@+id/center_view" // Központi elem ID
        app:layout_constraintCircleRadius="150dp" // Kör sugara
        app:layout_constraintCircleAngle="0" /> // Szög fokban (0° - jobbra)

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gomb 2"
        app:layout_constraintCircle="@+id/center_view"
        app:layout_constraintCircleRadius="150dp"
        app:layout_constraintCircleAngle="90" /> // 90° - lefelé

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gomb 3"
        app:layout_constraintCircle="@+id/center_view"
        app:layout_constraintCircleRadius="150dp"
        app:layout_constraintCircleAngle="180" /> // 180° - balra

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gomb 4"
        app:layout_constraintCircle="@+id/center_view"
        app:layout_constraintCircleRadius="150dp"
        app:layout_constraintCircleAngle="270" /> // 270° - fel

</androidx.constraintlayout.widget.ConstraintLayout>

A tulajdonságok a kör alakú pozícionáláshoz:

Tulajdonság Leírás
app:layout_constraintCircle Az elem ID-je, amely körül a kör épül.
app:layout_constraintCircleRadius A kör sugara dp-ben.
app:layout_constraintCircleAngle Szög fokban (0° - jobbra a központtól, az óramutató járásával ellentétesen növekszik).