1

我有一个垂直线性布局,它有两个不同元素的线性布局,我想将第一个固定到顶部,第二个居中,我正在尝试但不起作用:

<?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

为什么它不起作用?先感谢您

4

3 回答 3

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>

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

最好的祝愿,蒂姆

于 2012-05-25T08:03:07.777 回答
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>
于 2012-05-25T08:02:31.393 回答
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>
于 2012-05-25T08:01:14.810 回答