【问题标题】:Trying to center menu buttons试图使菜单按钮居中
【发布时间】:2012-10-02 01:43:33
【问题描述】:

我正在尝试垂直设置 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>

【问题讨论】:

标签: android


【解决方案1】:

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-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2014-04-18
    • 2021-04-08
    • 2012-08-29
    相关资源
    最近更新 更多