Android RelativeLayout:垂直对齐不适用

Android RelativeLayout: vertical align not apply

我在为后续布局应用垂直对齐时遇到问题,我不知道为什么。如果有人可以帮助我,请回复此post。谢谢!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp">

    <RelativeLayout android:id="@+id/ImgUp"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/piggy" />

    <RelativeLayout android:id="@+id/MainActivity"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@drawable/list_selector"
      android:layout_marginTop="10dp"
      android:layout_marginBottom="10dp"
      android:layout_below="@id/ImgUp" 
      android:layout_centerInParent="true">

      <TextView android:id="@+id/BarcodeOperation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/barcode"
        android:drawablePadding="5dp"
        android:text="@string/operation_barcode"
        android:gravity="center_vertical|center_horizontal"
        android:textSize="@dimen/large25"/>
    </RelativeLayout>

    <RelativeLayout android:id="@+id/ImgDown"
       android:layout_below="@id/MainActivity"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/piggy" />

    <RelativeLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content">

       <com.google.android.gms.ads.AdView
          android:id="@+id/adView" 
          android:layout_alignParentBottom="true"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          ads:adSize="BANNER"
          ads:adUnitId="@string/banner_ad_unit_id" >
      </com.google.android.gms.ads.AdView>

   </RelativeLayout>

</RelativeLayout>

这里是这个样子

这里是我想要的样子

有人可以帮助我吗?

非常简单:

设置中央View为centerInParent = "true".
然后在其上方设置一个View,在其下方设置一个View。

而且...不,真的,布局太多
相反,使用视图来支持性能。

我最终得到了这样的结果:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    >

    <!-- The "footer" (the ADs) -->
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"
    />

    <!-- The "central View" -->
    <TextView android:id="@+id/BarcodeOperation"
        android:background="@drawable/list_selector"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/barcode"
        android:drawablePadding="5dp"
        android:text="@string/operation_barcode"
        android:gravity="center_vertical|center_horizontal"
        android:textSize="@dimen/large25"
    />

    <!-- The "upper View" -->
    <ImageView
        android:id="@+id/ImgUp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/BarcodeOperation"
        android:background="@drawable/piggy"
    />

    <!-- The "lower View" -->
    <ImageView
        android:id="@+id/ImgDown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/BarcodeOperation"
        android:background="@drawable/piggy"
    />
</RelativeLayout>

请注意,我还用等效的 match_parent 替换了已弃用的属性 fill_parent(它需要 API Level 8+ 作为最低 SDK 版本 - 任何低于 Froyo 的都被认为是过时的并退出当前市场,如Dashboard).

所示