2016-02-12 47 views
2

我有一个问题,我不确定最好的方法来解决它。我需要在滚动视图中包含所有小部件。在scrollview中嵌套framelay?

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

enter image description here

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

ScrollView Inside ScrollView

这里的XML,如果有人可以给铅,我非常感谢。 thx!

<!-- 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> 

回答

3

我不知道我完全了解你的问题,但是,从我的理解:

你的目标

  • 您希望您的滚动型
  • 里面你的FrameLayout
  • 您的所有内容在ScrollView中都必须滚动,也就是说,您在屏幕上张贴的图片中的所有内容都必须滚动

从查看您的代码,我猜测您将FrameLayout和LinearLayout作为ScrollView的子项。 ScrollView只能有一个直接的孩子。 From Android documentation

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

解决方案

所以,你的情况,你想包装说的FrameLayout,并在另一ViewGroup中的LinearLayout中(可能是一个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只有一个直接的孩子,它里面的所有东西都会滚动。

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

干杯