如何获得android:layout_gravity =" bottom"在LinearLayout中工作

时间:2012-04-04 20:25:31

标签: android

我想在LinearLayout的顶部设置1个图像,在LinearLayout的底部设置1个。

但在我尝试的所有事情之后,我只能得到另一个下面的1个图像布局。我希望1对齐到顶部,1对齐到底部。

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/bg1"
        android:layout_gravity="top" />
    <ImageView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/bg2"
        android:layout_gravity="bottom" />
</LinearLayout>

我尝试过使用RelativeLayout(在主容器中)和layout_alignParentBottom(在我的第二张图片中),但这也无法解决我的问题。

谢谢你的任何想法。

2 个答案:

答案 0 :(得分:3)

以下RelativeLayout工作正常(已测试):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/ImageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@drawable/bg1" />

<ImageView
    android:id="@+id/ImageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/ImageView1"
    android:background="@drawable/bg2" />

如果仍有问题,请检查此RelativeLayout上方的容器。如果它是根,一切看起来都很好。

答案 1 :(得分:0)

这是解决问题的方法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/imgTop"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/bg1"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/imgTop"
        android:layout_above="@+id/imgBottom">

    </LinearLayout>


    <ImageView
        android:id="@id/imgBottom"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/bg2"
        android:layout_alignParentBottom="true" />

</RelativeLayout>