This view is not constrained

Рейтинг: 1Ответов: 1Опубликовано: 17.05.2023

Создаю приложение по туториал, не могу повторить действие автора. Возникла ошибка при создании двух tabItem внутри TabLayout. Опишу процесс: я добавляю tabItem в дерево компонентов, внутрь блока TabLayout. Сам TabLayout в режиме Design высвечивается, а вот два tabItem - нет. Выглядит вот так:

проблема визуально

Далее иду в код. Там внутри компонента TabLayout находятся два tabItem, открывающие теги которых подсвечиваются красной волной. Высвечиваемая ошибка: This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints. Сам код прикрепляю ниже

 <com.google.android.material.tabs.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp"
    android:backgroundTint="@color/blueColor"
    app:layout_constraintEnd_toEndOf="@+id/cardView"
    app:layout_constraintStart_toStartOf="@+id/cardView"
    app:layout_constraintTop_toBottomOf="@+id/cardView" />

<com.google.android.material.tabs.TabItem
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hours" />

<com.google.android.material.tabs.TabItem
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Days" />

Не знаю, что сделать, чтобы создать кнопки как на следующем скриншоте. Кстати, пыталась добавить дополнительно android:layout_marginEnd или ...marginTop и не помогло. Буду счастлива, если подскажете, что еще могу сделать!

введите сюда описание изображения

Ответы

▲ 2

Табы в TabLayout надо добавлять внутрь тэга, в его тело, а вы добавляете их не внутрь, а за пределами тела тэга)

У вас тэг TabLayout закрыт сразу после открытия (/>) и не имеет тела. Дайте ему тело и в него добавляйте табы:

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp"
    android:backgroundTint="@color/blueColor"
    app:layout_constraintEnd_toEndOf="@+id/cardView"
    app:layout_constraintStart_toStartOf="@+id/cardView"
    app:layout_constraintTop_toBottomOf="@+id/cardView">

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hours" />
    
    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Days" />

</com.google.android.material.tabs.TabLayout>