繁体   English   中英

在scrollview中嵌套framelayout?

[英]nesting framelayout inside a scrollview?

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

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

在此处输入图片说明

我看着这个线程,但这不是我想要的。

ScrollView内部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>

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

您的目标

  • 您想在ScrollView中使用FrameLayout
  • ScrollView中的所有内容都必须滚动,也就是说,您在图片中张贴的屏幕上的所有内容都必须滚动

通过查看您的代码,我您正在将FrameLayout和LinearLayout作为ScrollView的子级。 ScrollView只能有一个直接子代。 Android文档

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只有一个直接子级,并且其中的所有内容都会滚动。

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

干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM