android - 嵌套相对布局中的 layout_centerVertical 导致 Android P 上的 TextView 定位不正确

标签 android android-layout

<?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 为主题的 Activity 。

enter image description here

在其他 android 设备(非 Android P 设备)上,它被正确排列。

enter image description here

Android P 上出现这种错误排列的原因是什么?将外部布局更改为线性布局可以解决问题。为什么会这样?提前致谢。

最佳答案

您可以使用 ConstraintLayout 而不是 RelativeLayout 作为 TextView 的容器,以在所有 API 版本中实现您想要的效果。

注意 ConstraintLayout 作为 TextView 容器的用法,以及 app:layout_constraintTop_toBottomOf="@id/<your_id>" 等属性的用法.

要了解更多关于约束布局,请引用Build a Responsive UI with ConstraintLayout (Google Documentation)

<?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>

关于android - 嵌套相对布局中的 layout_centerVertical 导致 Android P 上的 TextView 定位不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52533120/

相关文章:

android - 使用按钮滚动图库

javascript - 除了react-native-background-task之外,还有什么方法可以在React Native中执行后台任务吗?

java - 以编程方式模拟从一个 Android 应用程序到另一个应用程序的用户输入

android - 使布局适应 Android 平台或 API 级别

android - FAB 和 ListView 的快速返回模式

android - 如何知道哪个手机支持哪个布局(hdpi、mdpi 和 xhpi)?

java - 将反斜杠字符分配给java String

android - Fused Location Provider 是个不错的选择吗?

android - 什么编解码器用于 android 中 AudioRecord.read() 的音频数据

java - viewpager 中的垂直 pagerTabStrip 方向