【问题标题】:nesting framelayout inside a scrollview?在滚动视图中嵌套框架布局?
【发布时间】:2016-02-12 18:17:16
【问题描述】:

我遇到了一个问题,我不确定最好的解决方法。我需要在滚动视图中包含所有小部件。

下面是布局,我使用了框架布局,因为我需要重叠一堆图像。然而,整个框架布局不能在滚动视图内。但我需要滚动整个面板。标有黄色箭头的部分表示框架布局。

我看过这个帖子,但这不是我想要的。

ScrollView Inside ScrollView

这是 xml,如果有人可以提供指导,我将不胜感激。 谢谢!

<!-- unfortunately I cannot put this inside the scrollview :( -->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image_background"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/follow_add" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:gravity="center|bottom"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/image_icon"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:background="@drawable/feed_active" />
    </LinearLayout>

    <ImageView
        android:id="@+id/dismiss"
        android:layout_gravity="left"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/close_messages_modal" />
    <ImageView
        android:id="@+id/follow"
        android:layout_gravity="right"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/follow_add" />
</FrameLayout>


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:text="Layout above needs to scroll"
            android:textColor="@color/half_dark_text_color"
            android:textSize="28sp"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:text="text1\ntext1\ntext1\ntext1\ntext1\ntext1\ntext1\ntext1\ntext1"
            android:textColor="@color/half_dark_text_color"
            android:textSize="18sp"
            android:textStyle="bold" />
        <ImageView
            android:id="@+id/id3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/close_messages_modal" />
        <ImageView
            android:id="@+id/share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@drawable/close_messages_modal" />
    </LinearLayout>
</ScrollView>

【问题讨论】:

    标签: android scrollview android-framelayout


    【解决方案1】:

    我不确定我是否完全理解了你的问题,但是,据我了解:

    您的目标

    • 您希望在 ScrollView 中包含 FrameLayout
    • 您在 ScrollView 中的所有内容都必须滚动,也就是说,您在图片中发布的屏幕中的所有内容都必须滚动

    通过查看您的代码,我猜测您将 FrameLayout 和 LinearLayout 作为 ScrollView 的子级。 ScrollView 只能有一个直接子级。来自Android documentation

    ScrollView 是 FrameLayout,这意味着您应该在其中放置一个包含要滚动的全部内容的子视图;这个孩子本身可能是一个具有复杂对象层次结构的布局管理器。一个经常使用的子元素是垂直方向的 LinearLayout,呈现用户可以滚动浏览的顶级项目的垂直数组。

    解决方案

    因此,在您的情况下,您希望像这样包装该 Framelayout 和 LinearLayout 在另一个 ViewGroup(可能是 LinearLayout)下:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
            <!--This LinearLayout wraps your frame and linear layouts so that they both stay into scrollview and are scrollable-->
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/image_background"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:background="@drawable/follow_add" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="100dp"
                    android:gravity="center|bottom"
                    android:orientation="horizontal">
                    <ImageView
                        android:id="@+id/image_icon"
                        android:layout_width="120dp"
                        android:layout_height="120dp"
                        android:background="@drawable/feed_active" />
                </LinearLayout>
    
                <ImageView
                    android:id="@+id/dismiss"
                    android:layout_gravity="left"
                    android:layout_margin="5dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/close_messages_modal" />
                <ImageView
                    android:id="@+id/follow"
                    android:layout_gravity="right"
                    android:layout_margin="5dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/follow_add" />
            </FrameLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10dp"
                    android:text="Layout above needs to scroll"
                    android:textColor="@color/half_dark_text_color"
                    android:textSize="28sp"
                    android:textStyle="bold" />
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="20dp"
                    android:text="text1\ntext1\ntext1\ntext1\ntext1\ntext1\ntext1\ntext1\ntext1"
                    android:textColor="@color/half_dark_text_color"
                    android:textSize="18sp"
                    android:textStyle="bold" />
                <ImageView
                    android:id="@+id/id3"
                    android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                    android:background="@drawable/close_messages_modal" />
                <ImageView
                    android:id="@+id/share"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:background="@drawable/close_messages_modal" />
            </LinearLayout>
            </LinearLayout>
        </ScrollView>
    

    这样一来,ScrollView 只有一个直接子级,并且里面的所有内容都会滚动。

    这应该足以实现您想要的。让我知道这是否是您真正想要的,至少这是我从您的帖子中理解的。

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多