layout_centerVertical в Nested Relative Layout, вызывающее некорректное позиционирование текстовых представлений на Android P - PullRequest
0 голосов
/ 27 сентября 2018

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/height"
        android:background="@color/orange7">

        <TextView
            android:padding="@dimen/padding_5dp"
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="@string/title" />

        <TextView
            android:padding="@dimen/padding_5dp"
            android:id="@+id/subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:text="@string/subtitle" />
    </RelativeLayout>
    <TextView
        android:layout_below="@id/header"
        android:padding="@dimen/padding_5dp"
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/description" />
</RelativeLayout>

У меня есть файл макета с вложенными относительными макетами.Только на устройствах Android P textviews во внутренней относительной компоновке размещены неправильно.Этот файл макета используется для действия с Theme.AppCompat.Dialog в качестве темы.

enter image description here

На других устройствах Android (не на устройствах Android P), его получениерасположены правильно.

enter image description here

Какая должна быть причина этого неправильного расположения на Android P?Изменение внешнего макета на линейный макет решает проблему.Почему это так?Заранее спасибо.

1 Ответ

0 голосов
/ 27 сентября 2018

Вы можете использовать ConstraintLayout вместо RelativeLayout в качестве контейнера для TextViews, чтобы достичь желаемого эффекта во всех версиях API.

Обратите внимание на использование ConstraintLayout в качестве контейнера текстовых представлений и использование таких атрибутов, как app:layout_constraintTop_toBottomOf="@id/<your_id>".

Чтобы узнать больше о макете ограничений, см. Создание отзывчивого пользовательского интерфейсас ConstraintLayout (Документация Google)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/height"
        android:background="@color/orange7">

        <TextView
            android:padding="@dimen/padding_5dp"
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/subtitle"
            android:text="@string/title" />

        <TextView
            android:padding="@dimen/padding_5dp"
            android:id="@+id/subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title"
            android:text="@string/subtitle" />

    </android.support.constraint.ConstraintLayout>
    <TextView
        android:layout_below="@id/header"
        android:padding="@dimen/padding_5dp"
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/description" />
</RelativeLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...