gpt4 book ai didi

android - 在 ScrollView 中嵌套框架布局?

转载 作者:搜寻专家 更新时间:2023-11-01 07:50:51 25 4
gpt4 key购买 nike

我遇到了一个问题,我不确定最好的解决方法。我需要将所有小部件都放在 ScrollView 中。

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

enter image description here

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

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>

最佳答案

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

您的目标

  • 您想将 FrameLayout 放在 ScrollView 中
  • 您在 ScrollView 中的所有内容都必须滚动,也就是说,您在图片中发布的屏幕中的所有内容都必须滚动

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

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

解决方案

因此,在您的情况下,您想像这样包装那个 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 只有一个直接 subview ,其中的所有内容都会滚动。

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

干杯

关于android - 在 ScrollView 中嵌套框架布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35369551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com