【发布时间】:2013-09-06 13:13:28
【问题描述】:
我的主 Activity 有许多按钮,它们在大平板电脑屏幕上彼此下方排列,但在较小的手机屏幕上不适合。我添加了一个 ScrollView,以便用户可以在屏幕尺寸需要时向下滚动到其他按钮:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bg"
android:gravity="center_horizontal">
<TextView
android:id="@+id/trackSelectorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo" />
<!-- More buttons -->
</LinearLayout>
</ScrollView>
</LinearLayout>
这有点工作,但是我在最后一个按钮下方有一个很大的空白区域(即 ScrollView 可以向下滚动太远)。无论是在实际平板电脑上还是在具有电话配置的模拟器上。如果我 center 内部的 LinearLayout(而不是 center_horizontal),空间会均匀分布在顶部和底部之间,我也不想要。如何修复此布局?
我希望这张图片能让它更清晰,想象一下 ScrollView 已经完全向下滚动并显示最后一个按钮。我的意思是右图Button 6下方的空白:
【问题讨论】:
-
你能画出你想要的吗?
-
完成了,我希望这能让可视化变得更容易。
-
尝试提供
ScrollViewandroid:layout_height="match_parent"。我怀疑空间在滚动视图内,但实际上在下面 -
是的 Lenart,将 ScrollView 包裹在垂直的 LinearLayout 周围,放置
maxWeightSum=3,并为每个按钮提供weigth=1。完成 :) LinearLayout 应该 fill_parent,滚动布局应该定义线性布局的视口大小。
标签: android android-scrollview