【问题标题】:android: ScrollView cuts off the topandroid: ScrollView 切掉顶部
【发布时间】:2019-09-12 16:53:47
【问题描述】:

所以我有一个带有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 的上半部分可见。甚至预览窗口也只显示了它在屏幕上的一半:

【问题讨论】:

    标签: android scrollview


    【解决方案1】:

    您将CardView 高度和ViewPager 高度都设置在match_parent 上,这将导致视觉问题,请尝试为它们设置一个固定的高度!

    【讨论】:

    • 如果我将高度设置为固定值(以编程方式或在 xml 中),问题仍然存在,仅看到 ViewPager 的上半部分。看看我的编辑。
    【解决方案2】:

    为了摆脱奇怪的偏移量(ViewPager 从屏幕上方开始),我必须从Linearlayout 中删除android:layout_gravity="center"。不知道为什么,如果有人向我解释,那将是涂料。可能有一些事情要做,我在LinearlayoutCardView 中使用了两次android:layout_gravity="center"

    然后我仍然以编程方式设置ViewPager 的高度,因此高度不为0。也不知道为什么,如果我在任何地方将match_parent 更改为wrap_content 都没关系。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 2018-06-23
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多