【问题标题】:NestedScrollView could not scroll with match_parent height childNestedScrollView 无法使用 match_parent 高度子滚动
【发布时间】:2016-09-09 09:56:33
【问题描述】:

我用片段实现 NonSwipeableViewPager 有像这样的 NestedScrollView,我期望滚动视图可以向上滚动并显示 2 个文本视图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                layout="@layout/header" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="16dp"
                android:src="@drawable/ic_up" />

        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 2" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

但无法滚动,我尝试了很多方法但仍然没有得到任何解决方案

【问题讨论】:

  • ScrollView 需要他们孩子的身高是 wrap_content
  • @TimCastelijns 所以我们没有办法使用 match_parent 高度,不是吗?
  • 这就是滚动视图的工作原理,您可以将滚动视图设置为匹配父视图,而不是滚动视图的子视图,需要包装内容

标签: android android-nestedscrollview


【解决方案1】:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

这个线性布局应该有android:layout_height="wrap_content"

原因是,如果滚动视图的子级与滚动视图本身大小相同(两者都为 match_parent 高度),则意味着没有任何内容可滚动,因为它们的大小相同,滚动视图只会和屏幕一样高。

如果线性布局的高度为wrap_content,则高度与屏幕高度无关,滚动视图将能够滚动浏览它。

请记住,滚动视图只能有 1 个直接子级,并且该子级需要 android:layout_height="wrap_content"

【讨论】:

  • 对我来说,只有fillViewPort = true 醒了。在我的情况下,孩子的身高并不重要
  • 是的,这里也一样!只需使用fillViewPort = true 就可以了。但是@Tim 有一点关于android:layout_height 的观点有时是正确的,过去曾遇到过问题。
  • FYI fillViewPort=true 将使滚动视图占用屏幕上的剩余空间,如果滚动视图本身的内容不
【解决方案2】:

在我的情况下app:layout_behavior="@string/appbar_scrolling_view_behavior" 这只有在有人遇到问题的情况下才有效,并且可能也可以解决您的问题。您还应该添加android:fillViewport="true",但如果没有这个我的代码工作。

 <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/subscription_background"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

【讨论】:

  • 我认为 app:layout_behavior 行为只有在我们使用 coordinatorlayout 时才适用。
【解决方案3】:

对我来说,当我为 androidx.core.widget.NestedScrollView 中的最后一个孩子添加“android:layout_marginBottom="100dp"”时它起作用了

【讨论】:

  • 不是解决方案,但我看到了同样的事情,我还没有理解为什么滚动视图不理解内容是可滚动的
  • 添加了同样的问题 "android:layout_marginBottom="200dp" 对我有用。
【解决方案4】:

如果你使用了 netedscrollview 如下你必须使用 android:scrollbars="垂直"

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/toolbar_updateUserDetails"
    app:layout_constraintBottom_toBottomOf="parent"
    android:fillViewport="true">
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="vertical"
        >
</LinearLayout>
</androidx.core.widget.NestedScrollView>

【讨论】:

    【解决方案5】:

    你必须计算你的另一个孩子,因为最后一个孩子绑定在 nestedScrollView 上。您添加边距,例如子高度。它正在工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      相关资源
      最近更新 更多