1

私は垂直方向の線形レイアウトを持っています。異なる要素を持つ2つの線形レイアウトの中にあります。最初のものを上に固定し、2番目のものを中央に固定したいのですが、試していますがうまくいきません:

<?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 に答える