Android 应用程序中无法解释的 white/blank space,未在布局预览中显示
Inexplicable white/blank space in Android app, not showing in layout preview
预览看起来不错,但应用 运行 不同。在布局检查器中,这个奇怪的白色 space 所在的地方什么也没有。好像是ConstraintLayout + ViewPager的问题。我在三个地方遇到了同样的问题,都具有相同的 ViewPager、0dp 垂直尺寸设置。我已经通过将 ConstraintLayouts 转换为 LinearLayouts 来解决。但问题是为什么我需要回归到 LinearLayout?我原来用ConstraintLayout和ViewPager写了UI,没问题。一路上的某个地方,在没有触及代码的情况下,事情变得很奇怪,这个空白 space 出现了。好像 Android UI 渲染中的某些东西在幕后发生了变化。任何人都知道它是什么,或者如何在保持 ConstraintLayout 的同时解决?
这是应用 运行ning 在设备上的布局检查器图像。我已经用我的鼠标书法把有问题的白色标记清楚了space
这是以下 xml 代码的布局预览,请注意 UI 元素填满屏幕,此视图底部工具栏后没有白色 space,这与实时应用程序不同。不要介意色差,应用程序会在 运行 次更改颜色。
fragment_layout.xml(在实时应用程序图像中绿色和标记为空白的 space 之间,UI 的其余部分来自 activity,但我知道这不是问题所在,所以不会为此添加 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="9.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,从子元素中删除所有约束并添加方向:"vertical"(当然)加上将 layout_weight: 1 放入 ViewPager 元素中。如果这对任何人有帮助。
我猜你正在使用 androidx.constraintlayout:constraintlayout:2.0.0-beta2
。
已知此特定版本会导致此类问题。
更新到最新版本(撰写本文时'androidx.constraintlayout:constraintlayout:2.0.0-beta6'
)。
预览看起来不错,但应用 运行 不同。在布局检查器中,这个奇怪的白色 space 所在的地方什么也没有。好像是ConstraintLayout + ViewPager的问题。我在三个地方遇到了同样的问题,都具有相同的 ViewPager、0dp 垂直尺寸设置。我已经通过将 ConstraintLayouts 转换为 LinearLayouts 来解决。但问题是为什么我需要回归到 LinearLayout?我原来用ConstraintLayout和ViewPager写了UI,没问题。一路上的某个地方,在没有触及代码的情况下,事情变得很奇怪,这个空白 space 出现了。好像 Android UI 渲染中的某些东西在幕后发生了变化。任何人都知道它是什么,或者如何在保持 ConstraintLayout 的同时解决?
这是应用 运行ning 在设备上的布局检查器图像。我已经用我的鼠标书法把有问题的白色标记清楚了space
这是以下 xml 代码的布局预览,请注意 UI 元素填满屏幕,此视图底部工具栏后没有白色 space,这与实时应用程序不同。不要介意色差,应用程序会在 运行 次更改颜色。
fragment_layout.xml(在实时应用程序图像中绿色和标记为空白的 space 之间,UI 的其余部分来自 activity,但我知道这不是问题所在,所以不会为此添加 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="9.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,从子元素中删除所有约束并添加方向:"vertical"(当然)加上将 layout_weight: 1 放入 ViewPager 元素中。如果这对任何人有帮助。
我猜你正在使用 androidx.constraintlayout:constraintlayout:2.0.0-beta2
。
已知此特定版本会导致此类问题。
更新到最新版本(撰写本文时'androidx.constraintlayout:constraintlayout:2.0.0-beta6'
)。