Android 滚动视图根本不滚动

Android 滚动视图根本不滚动,android,scroll,android-linearlayout,android-scrollview,Android,Scroll,Android Linearlayout,Android Scrollview,我无法使滚动视图正确滚动。它总是切断底部的内容,就好像它是一个正常的线性布局 我的代码 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewp

我无法使滚动视图正确滚动。它总是切断底部的内容,就好像它是一个正常的线性布局

我的代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout android:id="@+id/scroll_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:isScrollContainer="true"
        android:orientation="vertical" >

当然,我已经尝试添加/删除“fillViewport”和“isScrollContainer”属性,但它根本没有改变任何东西

这适用于ScrollView(仅垂直)
HorizontalScrollView必须用于水平滚动。

删除
LinearLayout
中的
android:isScrollContainer
。根据文档
android:isScrollContainer
用于设置视图可滚动。我希望它能帮助你。有关定义,请参阅此处。

回答:当用作XML布局的根元素时,ScrollView不起作用。必须将其包裹在线性布局中

解决方案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <LinearLayout android:id="@+id/scroll_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        </LinearLayout>
    </ScrollView>
</LinearLayout>


Android Studio将NestedScrollView添加到某些模板的活动文件中(例如,主详细信息)。在片段文件中有一个滚动视图,在该片段的活动文件中有另一个滚动视图,这会阻止滚动视图工作。删除片段文件中的ScrollView并保留活动文件中的ScrollView为我解决了问题。

尝试此解决方案,只需使用post delayed方法延迟滚动即可

fragment_test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:fresco="http://schemas.android.com/apk/res-auto">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="40dp"
        android:fillViewport="true"
        android:scrollbars="none">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            ...
        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

这对我很有效,希望能有所帮助。

这解决了我的问题:我在线性布局中添加了
android:isScrollContainer=“true”
,并删除了滚动视图 之前的代码:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ScrollView
    android:id="@+id/scrollViewRecords"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/sheepList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    </LinearLayout>
</ScrollView>
</LinearLayout>

之后的代码

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:isScrollContainer="true">

    <LinearLayout
        android:id="@+id/sheepList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    </LinearLayout>
</LinearLayout>

有没有办法解决这个问题?它是旧的,目前标记为有效的答案没有意义。关于ScrollView,目前唯一有效的方法是:

允许滚动放置在其中的视图层次结构的视图组滚动视图中只能放置一个直接子视图。若要在滚动视图中添加多个视图,请将直接子视图设置为添加视图组(例如LinearLayout),然后在该LinearLayout中放置其他视图。 切勿将RecyclerView或ListView添加到滚动视图中。这样做会导致较差的用户界面性能和较差的用户体验


摘自官方文件:

所选答案不正确

您可以使用ScrollView作为根视图,它不适合您,因为您缺少填充

添加如下内容:

android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"

我终于明白了,我花了5个小时一步一步地构建所有东西,然后一步一步地测试

无论如何,如果您对此有问题,我发现setOntouch侦听器正在弄乱您的代码-至少在我的情况下是


我必须想办法解决这个问题,但是试着删除你的ontouch侦听器并测试你的代码-它必须工作,因为家长没有问题。当我们向scrollview的直接子级添加填充/边距时,我遇到了这样的问题。保持scrollview的子级仅具有高度和宽度属性,这样就可以正常工作

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true"
            android:orientation="vertical">
</LinearLayout>
</ScrollView>

试试这个

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:fillViewport="true" >

<LinearLayout android:id="@+id/scroll_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


......

❌尝试了上述所有答案,但我的问题仍然没有解决。 经过一些调试和更改后,我的问题得到了解决,即ScrollView被滚动

  • 我们不需要向ScrollView添加任何父元素来使其滚动(如这里的一些其他答案所述)
✔️解决方案是针对我的问题✔️

  • 将滚动视图的高度更改为0dp

    android:layout\u height=“0dp”

  • 将顶部和底部约束为父视图/各自的视图/图元

    app:layout_constraintTop_toBottomOf=“@+id/imageView4” app:layout\u constraintBottom\u toBottomOf=“parent”

最终的xml如下所示



请粘贴完整的xml,可能与您的线性布局有关。尝试将
android:layout\u height=“match\u parent”
更改为
android:layout\u height=“0dp”
并在
滚动视图上添加
android:layout weight=“1”
。只是尝试了一下,运气不佳。对于Constraint layout的用户,正如@SiddharthLele所建议的,将布局高度设置为0dp对甲烷储罐起到了作用!无论Android有多么愚蠢,我都不会想到这也是其中一个原因-\u-还需要将scrollView的
layout\u height
更新为
wrap\u content
这不是真的,你不能使用
scrollView
作为根元素,您可以使用
ScrollView
作为
XML
的根元素。使用它作为根元素没有问题。OMG我已经为此挣扎了一个小时了。这是金子,也为我工作过!非常感谢。一个人怎么知道多少填充物就足够了?我的意思是,因为所有的设备都不同,这不会导致问题吗?不要在布局中添加任何边距和填充。公认的答案已经回答了这个问题。此外,还应添加结束xml标记-这可能会被误认为是父布局中的两个布局。还要添加一些描述。关键点是添加底部约束
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:fillViewport="true" >

<LinearLayout android:id="@+id/scroll_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
......
</LinearLayout>
</ScrollView>
</LinearLayout>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@+id/imageView4"
    app:layout_constraintBottom_toBottomOf="parent">

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

           <!-- Loong content -->

    </LinearLayout>
</ScrollView>