【问题标题】:Android Layout Relative to LinearLayout相对于 LinearLayout 的 Android 布局
【发布时间】:2017-06-16 03:50:07
【问题描述】:

这有点简化,以使问题更容易提出。

我有一个垂直方向的 LinearLayout。我不允许修改 LinearLayout,但我想在 LinearLayout 中每个元素的右侧显示一个按钮。我尝试查看 ConstraintLayout 和 RelativeLayout,但我收到一条消息,因为它们不是兄弟姐妹,所以这不起作用。

当视图位于 LinearLayout 内时,如何在视图右侧显示按钮?

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<!--
    DO NOT MODIFY THIS SECTION!!!
-->

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_constraintTop_toTopOf="parent"
        android:layout_constraintBottom_toBottomOf="parent"
        android:layout_constraintLeft_toLeftOf="parent" >

        <Button
            android:id="@+id/a"
            android:text="A"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/b"
            android:text="B"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/c"
            android:text="C"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/d"
            android:text="D"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

<!--
    END OF DO NOT MODIFY SECTION!!!
-->

<!--
    PLACE THIS BUTTON TO THE RIGHT OF BUTTON C
-->
    <Button
        android:id="@+id/c_aux"
        android:text="C AUX"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_constraintLeft_toRightOf="@id/c" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 贴出你试过的代码。
  • 以RelativeLayout为根布局,使view为toRightOf的Vertical LinearLayout。
  • 这将与线性布局的右侧对齐,而不是包含的按钮。

标签: android android-layout android-linearlayout android-relativelayout android-constraintlayout


【解决方案1】:

因此,如果您想在视图右侧显示一个按钮,请使用 XML 参数 android:layout_toRightOf="@id/someIDhere"。 但请记住,为了使用此参数,您需要为您尝试用作引用的元素声明和 ID,您可以通过在任何视图中添加此参数来做到这一点:android:id="@ +id/新名称"

这里我们使用 + 号表示这是我们第一次声明 ID。

【讨论】:

  • 正如我所提到的,我尝试了这个,但我得到一个错误,即 someIDhere 不是同级,所以这不起作用。
【解决方案2】:

您可以在主 LinearLayout 中添加另一个 LinearLayout。子布局可以有水平方向。

结果:

像这样:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:visibility="visible"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:text="Button Left"
            android:textColor="@android:color/white"
            android:textAllCaps="false"
            android:background="@android:color/holo_blue_dark"
            android:layout_marginStart="16dp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="@dimen/btn_height" />

        <Button
            android:text="Button Right"
            android:textColor="@android:color/white"
            android:textAllCaps="false"
            android:background="@android:color/holo_blue_dark"
            android:layout_margin="16dp"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="@dimen/btn_height" />

    </LinearLayout>

    <Button
        android:text="Button Bottom"
        android:textColor="@android:color/white"
        android:textAllCaps="false"
        android:background="@android:color/holo_blue_dark"
        android:layout_margin="16dp"
        android:layout_width="match_parent"
        android:layout_height="48dp" />

</LinearLayout>

【讨论】:

  • 正如我所提到的,我不允许修改 LinearLayout,所以这个解决方案不能解决这个问题。 :-(
猜你喜欢
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
相关资源
最近更新 更多