相对布局 - layout_toRightOf

Relative layout - layout_toRightOf

我开始将 TableLayout 转换为 RelativeLayout。让我们对列使用 A、B,对行使用 1、2。我将 A2 BELOW A1 和 B2 设置为 A2 的 RIGHT。问题是 A2 显示在 A1 之上(例如在第一行)!我试过 align_baseline 设置为 A2 但它的行为方式相同。解决方案是设置 A2 BELOW A1.

能否请您解释一下这种行为,为什么我还必须设置 BELOW 属性?我以为我只需要将第一列的元素设置在上行下方,他们的邻居就会坐在他们旁边。

PS 我查看了可能已经有我的答案和类似问题的问题。

更新代码:

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:text="1"
    android:id="@+id/firstOperand"
    style="@style/KeypadButton" />

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/firstOperand"
    android:text="+"
    android:id="@+id/operator"
    style="@style/KeypadFunctionButton" />

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/operator"
    android:text="1"
    android:id="@+id/secondOperand"
    style="@style/KeypadButton" />

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/secondOperand"
    android:text="="
    android:id="@+id/equalView"
    style="@style/KeypadFunctionButton" />

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/equalView"
    android:text="2"
    android:id="@+id/resultView"
    style="@style/KeypadButton" />

<SeekBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/firstOperand"
    android:id="@+id/seekBar"
    android:layout_gravity="center_vertical"
    android:onClick=""
    />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/seekBar"
    android:id="@+id/progressImage"
    android:src="@drawable/ic_action_cat"
    android:layout_gravity="center"
    android:layout_alignParentRight="true" />

我必须向 progressImage 添加以下行,否则图像会显示在操作员按钮上。

    android:layout_below="@id/firstOperand"

layout_toRightOf、layout_below 等方式是它们对齐 - 对于大多数视图 - 您指定的视图的左上角。此外,RelativeLayout 属性倾向于尽可能少地影响元素 - toRightOf 只是移动 x 坐标,below 只是移动 y 坐标,等等。对于复杂的布局,你会倾向于至少需要 2 个属性。

就个人而言,我倾向于使用 layout_alignToplayout_alignLeft 等,因为它们更直观。

如果您想了解有关此特定 ViewGroup 的更多信息,这只是额外阅读:Android User Interface Design: Relative Layouts

在相对布局中,layout_toRightOf 属性设置元素的水平坐标。所以,你告诉它把它放在所需元素右侧的 X 轴上。它设置 x 坐标并且元素浮动在顶部,因为没有垂直位置的指导(假设是 y 坐标)。调用 layout_below 设置 y 坐标。