Android两个控件布局,占满父亲

我的mainactivity的布局文件,默认的是一个相对布局。我里面就两个控件,一个Button和一个ImageView。我想让Button紧贴着父亲的上面,ImageView占满剩下的区域。Button的高度就普通的就行。我贴上我的代码,老出错。一运行就崩掉。


<Button 
       android:id="@+id/mybtn"
       android:layout_alignParentTop="true"         
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="点我"/> 

    <ImageView
       android:id="@+id/image_view"  
       android:layout_below="@id/mybtn"
       android:layout_marginBottom="0dp"
       android:scaleType="centerInside"
       />


ImageView没有设置width与height,肯定就会崩
其实要达到你说的效果,可以用垂直线性布局,ImageView高设为0dp,weight=1,width=match_parent

什么错误 ?

第一类:属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name” 
android:layout_below      在某元素的下方 
android:layout_above      在某元素的的上方 
android:layout_toLeftOf   在某元素的左边 
android:layout_toRightOf  在某元素的右边 

android:layout_alignTop   本元素的上边缘和某元素的的上边缘对齐 
android:layout_alignLeft  本元素的左边缘和某元素的的左边缘对齐 
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐 
android:layout_alignRight  本元素的右边缘和某元素的的右边缘对齐 

第三类:属性值为具体的像素值,如30dip,40px 
android:layout_marginBottom              离某元素底边缘的距离 
android:layout_marginLeft                   离某元素左边缘的距离 
android:layout_marginRight                 离某元素右边缘的距离 
android:layout_marginTop                   离某元素上边缘的距离 

学习了图片说明图片说明图片说明

ImageView没有设置width与height

ImageView设置android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/ic_launcher"

可以利用weight这个属性来分配。。。

这个错误也太低级了,,,,,width 和height 都是必须属性 你还没写,,,,,能不蹦?


<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <Button
            android:id="@+id/item_default_comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0" />


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
        android:src="@drawable/test_iv" />

    </LinearLayout>

第一类:属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边

android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离

给ImageView控件设置一下高和宽,没有这两这个属性肯定崩了。

你给ImageView加上:
android:layout_width="fill_parent"
android:layout_height="match_parent"

就是你要的了