相对布局位置

时间:2012-05-25 07:53:54

标签: android android-layout

我有一个垂直linearlayout,它有两个不同元素的linearlayout,我想将第一个修复到顶部,第二个居中,我正在尝试但是不起作用:

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

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_bg"
    >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="#3b5998"
    >    
         elements
    </LinearLayout>


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="300dip"
        android:layout_height="300dip"
        android:background="@drawable/background_resto"
        android:gravity="center"
    >

           elements
    </LinearLayout>
    </LinearLayout

为什么不起作用?提前谢谢

3 个答案:

答案 0 :(得分:3)

我建议为此目的选择RelativeLayout。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/holo_blue_bright"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:layout_alignParentTop="true"
        android:background="#fff" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="300dip"
        android:layout_height="300dip"
        android:layout_centerInParent="true"
        android:background="@android:color/holo_green_light"
        android:orientation="vertical" >
    </LinearLayout>
</RelativeLayout>

这将帮助您设置与其他视图相关的确切“相对”位置。

祝福, 添

答案 1 :(得分:2)

应使用RelativeLayout代替LinearLayout

<?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"
    android:background="@drawable/background_bg"
    >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="#3b5998"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        >
        elements
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="300dip"
        android:layout_height="300dip"
        android:background="@drawable/background_resto"
        android:layout_centerInParent="true"
        >
        elements
    </LinearLayout>

</RelativeLayout>

答案 2 :(得分:1)

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/background_bg"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:layout_alignParentTop="true"
            android:background="#ff0000" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="300dip"
            android:layout_height="300dip"
            android:layout_centerInParent="true"
            android:background="#ff0000"
            android:orientation="vertical" >
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>