2

我正在尝试垂直设置 2 个按钮的菜单。我将布局设置为 RelativeLayout 。第一个按钮以 android:layout_centerVertical="true" android:layout_centerHorizo​​ntal="true" 为中心,这是可行的。

当我尝试通过 android:layout_toBottomOf="@+id/menuat" 将第二个按钮添加到第一个按钮下时,它给了我一个错误。

我如何在屏幕上居中多个按钮?

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

  <Button
      android:id="@+id/menua"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:text="But A" 
  />

  <Button
      android:id="@+id/menub"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:layout_toBottomOf="@+id/menuat"
      android:text="But B" 
  />

</RelativeLayout>
4

1 回答 1

3

layout_toBottomOf不是有效参数。改为使用layout_below

这是修改后的代码:

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

  <Button
      android:id="@+id/menua"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:text="But A" 
  />

  <Button
      android:id="@+id/menub"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/menua"
      android:text="But B" 
  />

</RelativeLayout>

通过替换此代码,您将获得 menub 低于 menua

完整的相对位置是:

android:layout_toLeftOf
android:layout_toRightOf
android:layout_above
android:layout_below
于 2012-09-09T01:24:06.930 回答