android - 不需要的 FrameLayout 拉伸(stretch)以包裹所有 child 的高度(而不是只考虑一个 child 的高度)

标签 android android-linearlayout android-imageview android-xml android-framelayout

基本上,我使用一种流行的策略将 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/

相关文章:

android - 如何在 android 中以编程方式更新 linearlayout subview ?

android - ImageView src 与背景

android - 来自画廊的图像自动旋转 - Android

java - 登录 google play 游戏服务时出错 : looper. 无法调用 prepare()

Android Studio 无法构建新项目,等待从属 aapt 进程时超时

java - 修改 Settings.apk 以与 Project Glass 配合使用

android - 限制 Aviary 功能

java - 动态添加 ScrollView 到线性布局

Android:如何以编程方式设置布局的大小

android - ImageView 填充 parent 的宽度或高度,但保持纵横比