【发布时间】:2020-10-01 18:16:12
【问题描述】:
预览看起来不错,但应用的运行方式不同。在布局检查器中,这个奇怪的空白区域根本就没有。 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>
我的线性布局解决方案只是简单地将 ConstraintLayout 替换为 LinearLayout,从子元素中移除所有约束并添加方向:“vertical”(当然)以及将 layout_weight: 1 放入 ViewPager 元素中。如果这对任何人都有帮助。
【问题讨论】:
标签: android android-layout android-viewpager whitespace android-constraintlayout