【问题标题】:FrameLayout inside of ScrollViewScrollView 内的 FrameLayout
【发布时间】:2016-03-18 17:53:39
【问题描述】:

我是 android 开发新手,遇到以下问题。 我需要在ScrollView 内使用FrameLayout 使其可滚动。这是我写的

    <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" 
    android:background="#00ff00">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:background="#ff0000">
    </FrameLayout>
   </ScrollView>

但这不起作用。我在RelativeLayout 中尝试过,它有效,但我需要用于FrameLayout。 有什么想法吗?

【问题讨论】:

    标签: android layout


    【解决方案1】:

    android:fillViewport="true" 解决了这个问题。

    android:fillViewport, 定义滚动视图是否应该拉伸其内容以填充视口。

        <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
        <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </FrameLayout>
       </ScrollView>
    

    【讨论】:

    • 谢谢,解决了我的问题! FrameLayout 一直在自动调整。
    【解决方案2】:

    我尝试了许多变体来解决这个问题,包括这里的答案,但没有一个有用。 ScrollView 总是包裹FrameLayout,但是RelativeLayout 一切正常,LinearLayout...

    我为此找到了一种解决方案 - 将最小高度设置为 FrameLayout。您可以通过setMinimumHeight() 方法动态设置最小高度。

    【讨论】:

      【解决方案3】:

      要在 ScrollView 中获取 FrameLayout,请执行以下操作:

      <ScrollView
          android:id="@+id/scrollView1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_below="@+id/linearLayoutHeader1"
          android:layout_centerHorizontal="true" >
      
          <LinearLayout
              android:id="@+id/LinearLayout1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" >
              <FrameLayout
                  android:id="@+id/FrameLayout1"
                  android:layout_width="match_parent"
                  android:layout_height="1440dp"
                   >
              </FrameLayout>
      
          </LinearLayout>
      </ScrollView>
      

      【讨论】:

        【解决方案4】:

        您的 xml 示例非常好,我唯一能看到的是:如果 ScrollLayout 父级大于 500dp,它将不会滚动。

        确实,只有当内容大于滚动视图时才会使用滚动。

        在这里,您将内容高度设置为 500dip,并将滚动视图设置为“match_parent” 如果此 scrollView 的父级占据整个屏幕,在 800dip 高度的屏幕上(例如)

        => 根本不需要滚动。

        【讨论】:

          【解决方案5】:

          尝试在 FrameLayout 中将 layout_height 设置为“wrap_content”

          <FrameLayout
                  android:id="@+id/frameLayout"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="#ff0000">
          

          当 FrameLayout 高于屏幕边框时会滚动。或者,如果您想要可滚动区域的固定高度,请在 ScrollView 上将高度设置为 500dp。这取决于您想要什么,但永远不要在 ScrollView 子项上设置固定高度。 ScrollView 子项的高度应始终为 wrap_content。

          <ScrollView
              android:id="@+id/scrollView1"
              android:layout_width="match_parent"
              android:layout_height="500dp"
              android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true" 
              android:background="#00ff00">
          
              <FrameLayout
                  android:id="@+id/frameLayout"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="#ff0000">
              </FrameLayout>
          </ScrollView>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-04-15
            • 1970-01-01
            • 1970-01-01
            • 2015-06-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多