基本上,我使用一种流行的策略将 LinearLayout
的背景图像居中。通过将其包装在 FrameLayout
中并添加一个使用 fill_parent
的 ImageView在 LinearLayout
之前.但是,当我添加 ScrollView
在游戏中,一切都变了。
问题是图像在垂直方向上非常大并且 ScrollView
尊重 ImageView
的高度和 LinearLayout
由于 wrap_content
高度属性。当图像在垂直方向上大于它时,这会导致 LinearLayout 下方出现不需要的空白区域。
如何制作 FrameLayout
的高度只延伸到子 LinearLayout 的高度?如果可能,不要在运行时/绘制时以编程方式修改其大小。
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/cash_bg" />
<LinearLayout
android:id="@+id/main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<!-- My main views are here -->
</LinearLayout>
</FrameLayout>
</ScrollView>
最佳答案
我遇到了同样的问题,疯狂地寻找答案。我认为这是一个 Android 错误。我所做的是将 FrameLayout 包含到 LinearLayout 中,以便能够在 FrameLayout 的高度上强制“fill_parent”,这增加了复杂性,但解决了问题。没有找到任何其他(更好)的解决方案。 试试这个:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/cash_bg" />
<LinearLayout
android:id="@+id/main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<!-- My main views are here -->
</LinearLayout>
</FrameLayout>
</LinearLayout>
</ScrollView>
关于android - 不需要的 FrameLayout 拉伸(stretch)以包裹所有 child 的高度(而不是只考虑一个 child 的高度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12552757/