Как поместить фоновые фигуры в ConstraintLayout? - PullRequest
0 голосов
/ 24 октября 2019

Я вложил LinearLayouts и, прочитав какой-то документ, попытался переключиться на ConstraintLayout. Но я не нашел, как вставить представление, чтобы просто отобразить фон.
Пока я просто установил фон на макете, содержащем элементы.

Так выглядит старая вложенная структура (сбольше вложенных элементов в других вложенных макетах):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/black"
    android:gravity="center" >

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     ... some margins and padding
     android:background="@drawable/global_background" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:background="@drawable/text_area_background" >

        <TextView
        android:id="@+id/Text1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center" />

        <TextView
        android:id="@+id/Text2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center" />
    ...

Таким образом, я смог легко установить собственную форму в качестве фона для различных групп отображаемых элементов.

Как я могу получить эквивалент, используя ConstraintLayout?

Я понимаю, как я могу ограничить свои элементы, чтобы они были организованы таким же образом, и как я мог настроить фоны так, чтобы они соответствовали всему. Но как мне вставить простой фоновый элемент ?

1 Ответ

1 голос
/ 29 октября 2019

Я использовал ImageView для отображения своего фона и (вряд ли) нашел способ собрать свои предметы внутри него.
Одна проблема заключается в том, что я получаю предупреждение Image without contentDescription.

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

    <ImageView
        android:id="@+id/background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="2:5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/global_background" />

    ...

    <Button
        android:id="@+id/buttonValidate"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="2dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/green_button"
        android:text="@string/validate"
        app:layout_constraintBottom_toBottomOf="@+id/background"
        app:layout_constraintDimensionRatio="15:6"
        app:layout_constraintEnd_toEndOf="@+id/background"
        app:layout_constraintStart_toStartOf="@+id/background"
        app:layout_constraintTop_toBottomOf="@+id/upperButton" />

Но я изо всех сил пытался поместить другой фон внутри (сам содержащий несколько TextView). Мои ограничения всегда мешали одному или другому ... Я закончил с вложенным ConstraintLayout, который работает нормально, вероятно, не оптимальное решение, но все мои другие попытки потерпели неудачу.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...