android: ScrollView 切掉顶部

android: ScrollView cuts off the top

所以我有一个 ScrollView 的布局。当我测试我的代码时,ViewPager-heigth 从一个合理的值 (1845) 跳到非常小的值(快到肉眼看不到)。如果我将 ViewPagerlayout_height 切换为 wrap_content,高度将跳转到 0。

此外,当ViewPager的高度>0时,它的上半部分在屏幕外,只有下半部分可见。

很明显我在这里做错了:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/colorDeadBackground"
    android:fillViewport="true">

<LinearLayout
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:outlineProvider="bounds"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">
        <androidx.cardview.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:elevation="2dp"
            app:cardCornerRadius="6dp">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewpager_pics"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <com.finder.ViewPagerIndicator.LinePageIndicator
            android:id="@+id/indicator"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginTop="4dp"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            app:strokeWidth="2dp"
            app:unselectedColor="#88888888"
            app:selectedColor="@color/colorWhite"/>

        </androidx.cardview.widget.CardView>
...

如果我将高度设置为固定值(以编程方式或在 xml 中),问题仍然存在,即只能看到 ViewPager 的上半部分。即使是预览 Window 也只在屏幕上显示了一半:

您正在 match_parent 上设置 CardView 高度和 ViewPager 高度,这会导致可见性问题,请尝试为它们设置固定大小的高度!

为了摆脱奇怪的偏移(ViewPager 从屏幕上方开始),我不得不从 Linearlayout 中删除 android:layout_gravity="center"。不知道为什么,如果有人向我解释,那就太棒了。可能有一些事情要做,我在 LinearlayoutCardView.

中使用了两次 android:layout_gravity="center"

然后我仍然想以编程方式设置 ViewPager 的高度,这样高度就不是 0。也不知道为什么,如果我将 match_parent 更改为 wrap_content任何地方。