Ich lerne, wie man ConstraintLayout
verwendet. Ich möchte Menü mit 4 Elementen machen.ConstraintLayout View size - ratio with übergeordnete
Elemente in Mutter zentriert werden sollen
Elemente sollen Größe 1: 5 von Mutterhöhe (Verhältnis 1: 1 - Quadrat) so etwas wie diese
ich gemacht habe:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:id="@+id/split"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6" />
<android.support.constraint.ConstraintLayout
android:id="@+id/tiles_container"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toTopOf="@+id/split"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<android.support.constraint.Guideline
android:id="@+id/container_v"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<android.support.constraint.Guideline
android:id="@+id/container_h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<ImageView
android:id="@+id/tile_1"
android:layout_width="@dimen/tile_size"
android:layout_height="@dimen/tile_size"
android:layout_marginBottom="@dimen/tile_margin"
android:layout_marginEnd="@dimen/tile_margin"
app:layout_constraintBottom_toTopOf="@+id/container_v"
app:layout_constraintEnd_toStartOf="@+id/container_h"
app:srcCompat="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/tile_2"
android:layout_width="@dimen/tile_size"
android:layout_height="@dimen/tile_size"
android:layout_marginBottom="@dimen/tile_margin"
android:layout_marginStart="@dimen/tile_margin"
app:layout_constraintBottom_toTopOf="@+id/container_v"
app:layout_constraintStart_toStartOf="@+id/container_h"
app:srcCompat="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/tile_3"
android:layout_width="@dimen/tile_size"
android:layout_height="@dimen/tile_size"
android:layout_marginTop="@dimen/tile_margin"
android:layout_marginEnd="@dimen/tile_margin"
app:layout_constraintEnd_toStartOf="@+id/container_h"
app:layout_constraintTop_toTopOf="@+id/container_v"
app:srcCompat="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/tile_4"
android:layout_width="@dimen/tile_size"
android:layout_height="@dimen/tile_size"
android:layout_marginTop="@dimen/tile_margin"
android:layout_marginStart="@dimen/tile_margin"
app:layout_constraintStart_toStartOf="@+id/container_h"
app:layout_constraintTop_toTopOf="@+id/container_v"
app:srcCompat="@mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
Leider musste ich Bilder Breite und Höhe fest codieren. Ist es möglich, die Größe des Kindes basierend auf dem Verhältnis zum Elternteil festzulegen (wie in Xcode)
Implementierung 'com.android.support.constraint: constraint-layout: 1.1.0-beta3' –
"Sie sollten niemals ein ConstraintLayout in einem anderen verschachteln" Können Sie erklären, warum Sie das nicht tun sollten? Oder zeigen Sie die Dokumentation warum? Ich sehe nicht, wie Sie komplexere Ansichten erstellen könnten, ohne dies zu tun. – lespommes
Mobilgeräte aktualisieren ihre Anzeige mit 60 fps. Ihre App hat daher 16 ms Zeit, um jeden Frame zu aktualisieren. Wenn ein Frame-Rendering diesen Grenzwert überschreitet, wird es gelöscht und Sie sehen LAG. Verschachtelte Layouts benötigen mehr Zeit zum Rendern als flache, weshalb sie für die Leistung schlecht sind. Es ist noch schlimmer, wenn Sie ein RelativeLayout- oder ein LinearLayout-Objekt verschachteln, das Gewicht verwendet, da diese zwei Aufrufe bei jedem Rendern zweimal aufrufen. Es ist also viel schneller, wenn wir eine flache Layout-Hierarchie haben. –