-1

这有点简化,使问题更容易提出。Android布局相对于LinearLayout

我有一个垂直方向的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> 
+1

后你已经尝试了代码。 –

+0

以RelativeLayout作为根布局,并将视图设置为竖直LinearLayout的Right。 –

+0

这将对齐到线性布局的右侧,而不是包含的按钮。 – Mitch

回答

0

所以,如果你想显示一个按钮来视图的右侧,使用XML PARAM 机器人:layout_toRightOf = “@ ID/someIDhere”。 但请记住,为了使用此参数,您需要声明和ID为您尝试用作参考的元素,您可以通过在任何视图中添加此参数来执行此操作:android:id =“@ + id/newname“

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

+0

正如我所提到的,我尝试了这一点,并且我得到了一个错误,指出someID不是兄弟姐妹,所以这是行不通的。 – Mitch

0

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

结果:

enter image description here

像这样:

<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> 
+0

正如我所提到的,我不允许修改LinearLayout,所以这个解决方案没有解决这个问题。 :-( – Mitch