繁体   English   中英

ScrollView内部的FrameLayout

[英]FrameLayout inside of ScrollView

我是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:fillViewport =“true”解决了这个问题。

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

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

我尝试了很多变种来解决这个问题,包括这里的答案,但是没有人帮忙。 ScrollView总是包装FrameLayout ,但是RelativeLayoutLinearLayout ......一切正常......

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

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

实际上,仅当内容大于滚动视图时才使用滚动。

在这里,您将内容高度设置为500dip,如果此scrollView的父级在800dip高度屏幕上(例如)滚动整个屏幕,则将其滚动到'match_parent'

=>根本不需要滚动。

尝试在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>

要在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>

暂无
暂无

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

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