相对布局中的wrap_content是什么意思?

4

我有一个简单的布局,即使我将所有属性设置为wrap_content,结果布局在高度上也填满整个屏幕。单独的布局本身很小。因此,顶级容器应该只足够大以包装其所有元素。但它似乎填充了整个屏幕。在宽度方面,相同的属性可以正常工作。我错过了什么?

<RelativeLayout 
    android:id="@+id/topcontainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/solid_red"
>

<LinearLayout
    android:id="@+id/lowercontainer"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dip"
    android:layout_centerHorizontal="true"
    android:paddingLeft="0dip"
    android:paddingRight="0dip"
    android:layout_alignParentBottom="true"
    android:background="@drawable/solid_blue"
>

<ImageView
     android:id="@+id/lefticon"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:background="@drawable/ic_settings_display"
     android:layout_alignParentLeft="true"
     android:layout_alignParentBottom="true"
     android:paddingRight="0dip"
/> 

<ImageView
     android:id="@+id/righticon"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:background="@drawable/ic_settings_display"
     android:layout_alignParentRight="true"
     android:layout_alignParentBottom="true"
     android:paddingLeft="0dip"
/> 

</LinearLayout>

   <TextView android:id="@+id/test"
        android:text="Test text"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:paddingTop="10dip"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:textSize="20dip"
        android:layout_above="@id/lowercontainer"
        android:background="@drawable/solid_green"
   />         

</RelativeLayout>

alt text

1个回答

5

在您的LinearLayout中,android:layout_alignParentBottom="true"导致RelativeLayout拉伸到屏幕底部。


它是RelativeLayout。它有以下属性:android:id="@+id/topcontainer",android:layout_width="wrap_content",android:layout_height="wrap_content",android:background="@drawable/solid_red"。 - android_devrah
抱歉,我看到你在编辑帖子后立即发布了它。请尝试发布整个资源文件。 - Jon Willis
由于某种原因,顶层布局没有显示出来!但我已经在上面的注释中放置了其余的布局。我还附加了一张图片以展示我的情况。 - android_devrah
是你的LinearLayout中的android:layout_alignParentBottom="true"导致RelativeLayout一直延伸到屏幕底部。 - Jon Willis
非常感谢!确实是它造成了问题! - android_devrah
3
很棒,你找到了罪魁祸首。现在,你如何解决这个问题?我也遇到了同样的问题... - Graham Borland

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接