ScrollView 不适用于我的布局

ScrollView is not working for my layout

下面是我的 xml 文件,其中包含 ScrollView 但滚动不起作用,而且当我 运行 我的程序 RecyclerView(底部)项目从 UI 显示中丢失时移除 ScrollView 时显示。

<?xml version="1.0" encoding="utf-8"?>

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:background="#fff">

    <ImageView
        android:id="@+id/movie_image"
        android:layout_width="250px"
        android:layout_height="300px"
        android:layout_alignParentLeft="true"
        android:scaleType="centerCrop"
        android:src="@drawable/jellybean"/>

    <!-- title Label -->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/movie_image"
        android:paddingLeft="20dip"
        android:textColor="@color/black"
        android:text="Title "
        android:singleLine="true"
        android:ellipsize="end"
        android:textSize="18sp"
        android:textStyle="bold" />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/popularity_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingBottom="10dp"
        android:orientation="horizontal"
        android:layout_toRightOf="@+id/movie_image"
        android:layout_below="@+id/title">

        <!-- popularity label -->
        <ImageView
            android:id="@+id/star1"
            android:layout_width="45px"
            android:layout_height="45px"
            android:layout_toRightOf="@+id/popularity"
            android:src="@drawable/blank_star" />
        <ImageView
            android:id="@+id/star2"
            android:layout_width="45px"
            android:layout_height="45px"
            android:layout_toRightOf="@+id/star1"
            android:src="@drawable/blank_star"/>
        <ImageView
            android:id="@+id/star3"
            android:layout_width="45px"
            android:layout_height="45px"
            android:layout_toRightOf="@+id/star2"
            android:src="@drawable/blank_star"/>
        <ImageView
            android:id="@+id/star4"
            android:layout_width="45px"
            android:layout_height="45px"
            android:layout_toRightOf="@+id/star3"
            android:scaleType="centerCrop"
            android:src="@drawable/blank_star"/>
        <ImageView
            android:id="@+id/star5"
            android:layout_width="45px"
            android:layout_height="45px"
            android:layout_toRightOf="@+id/star4"
            android:src="@drawable/blank_star"/>

    </RelativeLayout>

    <!-- tagline label -->
    <TextView
        android:id="@+id/tagline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="tagline"
        android:paddingLeft="20dip"
        android:layout_toRightOf="@+id/movie_image"
        android:layout_below="@+id/popularity_list"
        android:textColor="@color/black" />

    <!-- release date label -->
    <TextView
        android:id="@+id/release_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20dip"
        android:layout_toRightOf="@+id/movie_image"
        android:layout_below="@+id/tagline"
        android:text="date "
        android:textColor="@color/black" />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/budget_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dip"
        android:orientation="horizontal"
        android:layout_toRightOf="@+id/movie_image"
        android:layout_below="@+id/release_date">

        <!-- Budget label -->
        <TextView
            android:id="@+id/budget_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Budget : "
            android:textColor="@color/black" />
        <TextView
            android:id="@+id/budget"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/budget_text"
            android:text="budget "
            android:textColor="@color/black" />
    </RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/revenue_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dip"
        android:orientation="horizontal"
        android:layout_toRightOf="@+id/movie_image"
        android:layout_below="@+id/budget_group">

        <!-- Revenue label -->
        <TextView
            android:id="@+id/revenue_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Revenue : "
            android:textColor="@color/black" />
        <TextView
            android:id="@+id/revenue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/revenue_text"
            android:text="revenue "
            android:textColor="@color/black" />
    </RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/status_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dip"
        android:orientation="horizontal"
        android:layout_toRightOf="@+id/movie_image"
        android:layout_below="@+id/revenue_group">

        <!-- Movie status label -->
        <TextView
            android:id="@+id/status_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Status : "
            android:textColor="@color/black" />
        <TextView
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/status_text"
            android:text="status "
            android:textColor="@color/black" />
    </RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Vote_count_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="10dip"
        android:layout_below="@+id/movie_image">

        <!-- favourites label -->
        <ImageView
            android:id="@+id/favourites"
            android:layout_width="50px"
            android:layout_height="50px"
            android:scaleType="centerCrop"
            android:src="@drawable/blank_star"/>

        <TextView
            android:id="@+id/opening_brac"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/favourites"
            android:paddingLeft="10dip"
            android:text="("
            android:textColor="@color/black" />

        <!-- Vote average label -->
        <TextView
            android:id="@+id/votes_avg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/opening_brac"
            android:text="Votes_avg "
            android:paddingBottom="2dip"
            android:textColor="@color/black" />

        <TextView
            android:id="@+id/after_vote_avg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/votes_avg"
            android:text="/10)"
            android:textColor="@color/black" />

        <!-- Total vote counts label -->
        <TextView
            android:id="@+id/votes_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/after_vote_avg"
            android:layout_toRightOf="@+id/favourites"
            android:paddingLeft="10dip"
            android:text="count"
            android:textColor="@color/black" />

        <TextView
            android:id="@+id/closing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/after_vote_avg"
            android:layout_toRightOf="@+id/votes_count"
            android:text=" users."
            android:textColor="@color/black" />

        <!-- Overview label -->
        <TextView
            android:id="@+id/overview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/closing"
            android:text="Overview"
            android:paddingTop="20dp"
            android:textColor="@color/black" />
    </RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fav_watchlist_icons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="10dip"
        android:gravity="center"
        android:layout_below="@+id/Vote_count_list">

        <!-- My favourite icon and text group -->
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/fav_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="10dip">

            <ImageView
                android:id="@+id/fav_icon"
                android:layout_width="160px"
                android:layout_height="160px"
                android:scaleType="centerCrop"
                android:src="@drawable/favorite_disable_normal"/>

            <TextView
                android:id="@+id/fav_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/fav_icon"
                android:paddingRight="20dp"
                android:paddingTop="10dp"
                android:text="My Favourite"
                android:textColor="@color/black" />
        </RelativeLayout>

        <!-- My watchlist icon and text group -->
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/watchlist_icons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="10dip"
            android:layout_toRightOf="@+id/fav_group">

            <ImageView
                android:id="@+id/watchlist_icon"
                android:layout_width="160px"
                android:layout_height="160px"
                android:scaleType="centerCrop"
                android:src="@drawable/watchlist_disable_normal"/>

            <TextView
                android:id="@+id/watchlist_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="20dp"
                android:paddingTop="10dp"
                android:text="My Watchlist"
                android:layout_below="@id/watchlist_icon"
                android:textColor="@color/black" />
        </RelativeLayout>

    </RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/poster_list"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="15dip"
        android:layout_below="@+id/fav_watchlist_icons">

        <!--  Recycler View  -->
        <android.support.v7.widget.RecyclerView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/recycler_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="none" />

    </RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/trailor_list"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5dip"
        android:layout_below="@+id/poster_list">

        <!--  Recycler View for Video links -->
        <android.support.v7.widget.RecyclerView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/recycler_view_trailers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="none" />

    </RelativeLayout>

    </RelativeLayout>

</ScrollView> 

我在其他帖子中看到 ScrollView 应该包裹在我已经完成的单个对象上,但它仍然无法正常工作。

尝试以下-

  1. 使所有相关布局成为线性布局的子布局(经过一些研究发现这是正确的方法)。
  2. 为您的 ScrollView 布局指定一个最小高度 (android:minHeight)。

应该可以。

我遇到了同样的问题。其实主要问题是scrollview.

Note: 不要使用 scrollview 作为父视图。只需在父布局内​​使用滚动视图,即 LinearLayout 或 RelativeLayout。

尝试这样的事情:

<?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:background="#fff">

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

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

        <ImageView
            android:id="@+id/movie_image"
            android:layout_width="250px"
            android:layout_height="300px"
            android:layout_alignParentLeft="true"
            android:scaleType="centerCrop"
            android:src="@drawable/jellybean"/>

        <!-- title Label -->
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/movie_image"
            android:paddingLeft="20dip"
            android:textColor="@color/black"
            android:text="Title "
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="18sp"
            android:textStyle="bold" />

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/popularity_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:paddingBottom="10dp"
            android:orientation="horizontal"
            android:layout_toRightOf="@+id/movie_image"
            android:layout_below="@+id/title">

            <!-- popularity label -->
            <ImageView
                android:id="@+id/star1"
                android:layout_width="45px"
                android:layout_height="45px"
                android:layout_toRightOf="@+id/popularity"
                android:src="@drawable/blank_star" />
            <ImageView
                android:id="@+id/star2"
                android:layout_width="45px"
                android:layout_height="45px"
                android:layout_toRightOf="@+id/star1"
                android:src="@drawable/blank_star"/>
            <ImageView
                android:id="@+id/star3"
                android:layout_width="45px"
                android:layout_height="45px"
                android:layout_toRightOf="@+id/star2"
                android:src="@drawable/blank_star"/>
            <ImageView
                android:id="@+id/star4"
                android:layout_width="45px"
                android:layout_height="45px"
                android:layout_toRightOf="@+id/star3"
                android:scaleType="centerCrop"
                android:src="@drawable/blank_star"/>
            <ImageView
                android:id="@+id/star5"
                android:layout_width="45px"
                android:layout_height="45px"
                android:layout_toRightOf="@+id/star4"
                android:src="@drawable/blank_star"/>

        </RelativeLayout>

        <!-- tagline label -->
        <TextView
            android:id="@+id/tagline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="tagline"
            android:paddingLeft="20dip"
            android:layout_toRightOf="@+id/movie_image"
            android:layout_below="@+id/popularity_list"
            android:textColor="@color/black" />

        <!-- release date label -->
        <TextView
            android:id="@+id/release_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20dip"
            android:layout_toRightOf="@+id/movie_image"
            android:layout_below="@+id/tagline"
            android:text="date "
            android:textColor="@color/black" />

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/budget_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="20dip"
            android:orientation="horizontal"
            android:layout_toRightOf="@+id/movie_image"
            android:layout_below="@+id/release_date">

            <!-- Budget label -->
            <TextView
                android:id="@+id/budget_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Budget : "
                android:textColor="@color/black" />
            <TextView
                android:id="@+id/budget"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/budget_text"
                android:text="budget "
                android:textColor="@color/black" />
        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/revenue_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="20dip"
            android:orientation="horizontal"
            android:layout_toRightOf="@+id/movie_image"
            android:layout_below="@+id/budget_group">

            <!-- Revenue label -->
            <TextView
                android:id="@+id/revenue_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Revenue : "
                android:textColor="@color/black" />
            <TextView
                android:id="@+id/revenue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/revenue_text"
                android:text="revenue "
                android:textColor="@color/black" />
        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/status_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="20dip"
            android:orientation="horizontal"
            android:layout_toRightOf="@+id/movie_image"
            android:layout_below="@+id/revenue_group">

            <!-- Movie status label -->
            <TextView
                android:id="@+id/status_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Status : "
                android:textColor="@color/black" />
            <TextView
                android:id="@+id/status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/status_text"
                android:text="status "
                android:textColor="@color/black" />
        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/Vote_count_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="10dip"
            android:layout_below="@+id/movie_image">

            <!-- favourites label -->
            <ImageView
                android:id="@+id/favourites"
                android:layout_width="50px"
                android:layout_height="50px"
                android:scaleType="centerCrop"
                android:src="@drawable/blank_star"/>

            <TextView
                android:id="@+id/opening_brac"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/favourites"
                android:paddingLeft="10dip"
                android:text="("
                android:textColor="@color/black" />

            <!-- Vote average label -->
            <TextView
                android:id="@+id/votes_avg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/opening_brac"
                android:text="Votes_avg "
                android:paddingBottom="2dip"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/after_vote_avg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/votes_avg"
                android:text="/10)"
                android:textColor="@color/black" />

            <!-- Total vote counts label -->
            <TextView
                android:id="@+id/votes_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/after_vote_avg"
                android:layout_toRightOf="@+id/favourites"
                android:paddingLeft="10dip"
                android:text="count"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/closing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/after_vote_avg"
                android:layout_toRightOf="@+id/votes_count"
                android:text=" users."
                android:textColor="@color/black" />

            <!-- Overview label -->
            <TextView
                android:id="@+id/overview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/closing"
                android:text="Overview"
                android:paddingTop="20dp"
                android:textColor="@color/black" />
        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/fav_watchlist_icons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="10dip"
            android:gravity="center"
            android:layout_below="@+id/Vote_count_list">

            <!-- My favourite icon and text group -->
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/fav_group"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="10dip">

                <ImageView
                    android:id="@+id/fav_icon"
                    android:layout_width="160px"
                    android:layout_height="160px"
                    android:scaleType="centerCrop"
                    android:src="@drawable/favorite_disable_normal"/>

                <TextView
                    android:id="@+id/fav_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/fav_icon"
                    android:paddingRight="20dp"
                    android:paddingTop="10dp"
                    android:text="My Favourite"
                    android:textColor="@color/black" />
            </RelativeLayout>

            <!-- My watchlist icon and text group -->
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/watchlist_icons"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="10dip"
                android:layout_toRightOf="@+id/fav_group">

                <ImageView
                    android:id="@+id/watchlist_icon"
                    android:layout_width="160px"
                    android:layout_height="160px"
                    android:scaleType="centerCrop"
                    android:src="@drawable/watchlist_disable_normal"/>

                <TextView
                    android:id="@+id/watchlist_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingRight="20dp"
                    android:paddingTop="10dp"
                    android:text="My Watchlist"
                    android:layout_below="@id/watchlist_icon"
                    android:textColor="@color/black" />
            </RelativeLayout>

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/poster_list"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="15dip"
            android:layout_below="@+id/fav_watchlist_icons">

            <!--  Recycler View  -->
            <android.support.v7.widget.RecyclerView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/recycler_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbars="none" />

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/trailor_list"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dip"
            android:layout_below="@+id/poster_list">

            <!--  Recycler View for Video links -->
            <android.support.v7.widget.RecyclerView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/recycler_view_trailers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbars="none" />

        </RelativeLayout>
            </RelativeLayout>
        </ScrollView>
    </RelativeLayout>

ScrollView 只能有一个子视图或 ViewGroup。

在您的布局中,您有 2 个相对布局子项s.So将两者合并为一个 ViewGruop。

将 ScrollView 的宽度和高度设置为 fill_parent

Use android:fillViewPort:true in Scrollview

您可以使用android:fillViewport="true"让NestedScrollView测量RecyclerView。 RecyclerView 将填充剩余的高度。所以如果你想滚动 NestScrollView,你可以设置 RecyclerView 的 minHeight。

请参考以下内容link: How to use RecyclerView inside NestedScrollView?