我是新的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 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
,但一切正常与RelativeLayout
, LinearLayout
…
我find了一个解决scheme – 设置FrameLayout
最小高度。 您可以通过setMinimumHeight()
方法dynamic设置最小高度。
你的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>