<?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 。
在其他 android 设备(非 Android P 设备)上,它被正确排列。
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/