【问题标题】:Undesired FrameLayout Stretch to Wrap All Children's Heights (Instead of Respecting Just One Child's Height)不需要的 FrameLayout 拉伸以包裹所有孩子的身高(而不是只尊重一个孩子的身高)
【发布时间】:2012-09-23 13:31:56
【问题描述】:

基本上,我使用一种流行的策略将LinearLayout 的背景图像居中,方法是将其包装在FrameLayout 中,并在LinearLayout 之前添加一个使用fill_parent 的ImageView。然而,当我在游戏中添加ScrollView 时,一切都变了。

问题是图像在垂直方向上非常大,并且由于wrap_content 高度属性,ScrollView 尊重ImageViewLinearLayout 的高度。当图像垂直大于它时,这会导致 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 android-linearlayout android-imageview android-xml android-framelayout


    【解决方案1】:

    我遇到了同样的问题,疯狂地寻找答案。我认为这是一个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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-12
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多