繁体   English   中英

嵌套相对布局中的layout_centerVertical导致Android P上textviews的位置不正确

[英]layout_centerVertical in Nested Relative Layout causing incorrect positioning of textviews on Android P

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

在此处输入图片说明

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

在此处输入图片说明

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

您可以使用ConstraintLayout而不是RelativeLayout作为TextView的容器,以在所有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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM