android - Android应用程序中莫名其妙的空白/空白区域,未显示在布局预览中
问题描述
预览看起来不错,但应用程序的运行方式不同。在布局检查器中,这个奇怪的空白区域根本就没有。ConstraintLayout + ViewPager 似乎有问题。我在三个地方遇到了同样的问题,都使用相同的 ViewPager,0dp 垂直尺寸设置。我已经通过将 ConstraintLayouts 转换为 LinearLayouts 来解决。但问题是为什么我需要回归到 LinearLayout?我最初用 ConstraintLayout 和 ViewPager 编写 UI,这很好。一路上的某个地方,没有接触代码,事情变得很奇怪,这个空白出现了。好像 Android UI 渲染中的某些东西在幕后发生了变化。任何人都知道它是什么,或者在保持 ConstraintLayout 的同时如何解决?
这是在设备上运行的应用程序的布局检查器图像。我已经用我的鼠标书法技巧清楚地标记了有问题的空白区域。
这是以下 xml 代码的布局预览,注意 UI 元素填满屏幕,此视图中底部工具栏后没有空白,与实时应用不同。不要介意颜色差异,应用程序会在运行时更改颜色。
fragment_layout.xml(在实时应用图像中的绿色和标记的空白区域之间,UI 的其余部分来自活动,但我知道这不是问题,所以不会为此添加 xml)
<?xml version="1.0" encoding="utf-8"?>
<layout
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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.order.OrderBuildFragment"
>
<com.google.android.material.tabs.TabLayout
android:id="@+id/order_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:layout_constraintBottom_toTopOf="@+id/order_viewpager"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/order_viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/white"
app:layout_constraintTop_toBottomOf="@id/order_tabs"
app:layout_constraintBottom_toTopOf="@id/build_order_bottom_toolbar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
</androidx.viewpager.widget.ViewPager>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/build_order_bottom_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/order_viewpager"
app:layout_constraintBottom_toBottomOf="parent"
android:background="?attr/colorPrimary"
>
<TextView
android:id="@+id/order_service_point_spinner_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/service_point"
android:textColor="@color/white"
app:layout_constraintEnd_toStartOf="@id/order_toolbar_service_point_spinner"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/build_order_guideline_service_point_and_action_buttons"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
/>
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/order_toolbar_service_point_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/build_order_guideline_service_point_and_action_buttons"
app:layout_constraintStart_toEndOf="@id/order_service_point_spinner_label"
android:layout_marginEnd="8dp"/>
<androidx.constraintlayout.widget.Guideline
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/build_order_guideline_service_point_and_action_buttons"
app:layout_constraintGuide_percent="0.5"
/>
<TextView
android:id="@+id/order_textView_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@{@string/currency_symbol + viewModel.price}"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/order_toolbar_button_more"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/build_order_guideline_service_point_and_action_buttons"
tools:text="$999.99"
/>
<Button
android:id="@+id/order_toolbar_button_more"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="moreButtonAction"
android:text="@string/more"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/order_toolbar_button_send"
app:layout_constraintStart_toEndOf="@id/order_textView_price"
app:layout_constraintTop_toBottomOf="@id/build_order_guideline_service_point_and_action_buttons"/>
<Button
android:id="@+id/order_toolbar_button_send"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendButtonAction"
android:text="@{viewModel.sendButtonText}"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/order_toolbar_button_more"
app:layout_constraintTop_toBottomOf="@+id/build_order_guideline_service_point_and_action_buttons"
tools:text="@string/send"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/build_order_progress_bar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="@{safeUnbox(viewModel.loading)}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我的线性布局解决方案简单地包括用 LinearLayout 替换 ConstraintLayout,从子元素中删除所有约束并添加方向:“垂直”(当然)加上 layout_weight: 1 在 ViewPager 元素中。如果这对任何人都有帮助。
解决方案
我的猜测是您正在使用androidx.constraintlayout:constraintlayout:2.0.0-beta2
. 已知此特定版本会导致此类问题。
更新到最新版本('androidx.constraintlayout:constraintlayout:2.0.0-beta6'
在撰写本文时)。
推荐阅读
- asp.net-core - 部署包含敏感数据的文件的选项有哪些?
- css - flex布局如何自动扩展子元素间距?
- java - Java 并发修改异常(枚举器)
- java - 将 Put 方法返回 REST 中的任何内容,在 POSTMAN 拦截器中正常工作
- android - 带有 FLAG_UPDATE_CURRENT 的 PendingIntent.getActivity 不会更新意图的标志
- python-3.x - 如何使用 G-Suite Admin SDK Directory API 获取每个人的 Gmail?
- java - JavaFX:无法取消粗体 TableView ColumnHeader 标签文本
- spring-security - 我可以在 Spring SAML 应用程序中添加多个 IDP 吗?
- python - 如何按字母顺序排列文件中的内容
- javascript - $ 和 $$ 是作为 querySelector/All 的别名引入的吗?