Java 在Android中滚动活动中的所有元素

Java 在Android中滚动活动中的所有元素,java,android,xml,layout,Java,Android,Xml,Layout,我在Android中有一个活动,有两个主要视图:一个是带有一些文本框的标题布局,另一个是一个视图组,它是一个不等高元素网格的自身实现。如图所示: 我希望不仅能够滚动视图中的元素,而且能够滚动整个活动。我无法对列表使用setHeaderView,因为它不是ListView,而是ViewGroup的子类。我无法做到这一点:-( 以下是我尝试使用的代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi

我在Android中有一个活动,有两个主要视图:一个是带有一些文本框的标题布局,另一个是一个视图组,它是一个不等高元素网格的自身实现。如图所示:

我希望不仅能够滚动视图中的元素,而且能够滚动整个活动。我无法对列表使用setHeaderView,因为它不是ListView,而是ViewGroup的子类。我无法做到这一点:-(

以下是我尝试使用的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#E0E0DE"
    tools:context=".MainActivity" >

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/layout_action_bar" />

    <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"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin" >

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

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/background"
                    android:scaleType="centerCrop"
                    android:adjustViewBounds="true" />

                <include
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    layout="@layout/layout_search_view"
                    android:layout_centerInParent="true"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp" />

            </RelativeLayout>

            <include
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                layout="@layout/layout_location_info" />

            <com.tenea.turipolis.UnequalGridView
                android:id="@+id/listPictures"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                android:columnCount="2"
                android:padding="2dp"
                android:verticalSpacing="2dp"
                android:horizontalSpacing="2dp" />

        </LinearLayout>

    </ScrollView>

</LinearLayout>


感谢您的支持,

将您的整个版面用一条线环绕。

将您的版面用以下线包裹起来:

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

// your layout here

</ScollView>

//你的布局在这里
您只能有一个子小部件,例如:

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

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

    // your layout here

    </LinearLayout>

</ScollView>

//你的布局在这里

谢谢您的评论。“ScrollView只能有一个子小部件。如果您想要更多子部件,请将它们包装在容器布局中。”使用当前用作ScrollView唯一子部件的布局。这就是“环绕整个布局”的含义.Hi dst,这还不够,因为我需要拉伸视图组。如何才能滚动到视图组的末尾?我不确定是否理解您的评论…您的视图组和线性布局(或您使用的任何布局)应该有一个
layout\u height
wrap\u content
…或者在底部有控件吗?在这种情况下,.Hi Joe,谢谢你的评论。它可以工作,但这不是我想要做的。最后一件事是我需要将视图组拉伸到最后,以便能够滚动整个列表。我的意思是,通过你的方法,我可以可以滚动,但我需要固定视图组的高度。不是100%您需要的高度,如果看不到所有代码,这很难。尝试将线性布局中的布局高度更改为“填充父视图”?可能其他视图也需要该设置?将安卓:fillViewport=“true”添加到您的scrollview xml属性中