app:layout_marginBottom在使用Android约束布局时无法正常工作

50

以下的layout_marginBottom为什么不起作用?然而,如果我在第二个视图上使用layout_marginTop,它确实能够正常工作。

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ade4ad">
    <TextView
        android:id="@+id/first"
        android:layout_width="90dp"
        android:layout_height="40dp"
        app:layout_marginBottom="10dp"
        android:background="#000"/>
    <TextView
        android:id="@+id/second"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:background="#fff"
        app:layout_constraintTop_toBottomOf="@+id/first"/>
</android.support.constraint.ConstraintLayout>

我遇到了完全相同的问题,我通过在第一个textView中添加以下约束条件来解决它:app:layout_constraintBottomToBottomOf="@+id/second"。然后marginBottom就正常工作了。 - Rémi P
1
你需要有一个约束条件才能有边距。例如,对于底部边距,必须有底部约束条件。其他边也是类似的。 - Siva Prakash
谢谢Shiva,这是正确的答案。 - Vikas Pandey
4个回答

64

为了实现这一目标,我们需要采取一些重要的措施。

android:layout_marginBottom="20dp" 

要发挥良好的效果,您应该使用

app:layout_constraintBottom_toBottomOf="parent"

15
如果您想让顶部和底部边距被考虑,就必须对所有视图应用顶部和底部的约束条件。同样的,如果想要左右边距被考虑,就得应用左右约束条件来限制视图位置。 - Etienne Lawlor

27

只有在以下情况下,布局的顶部/底部边距才会生效:

  1. 同一方向上的约束需要与其下一个相邻子项连接,就像单向链表一样。
  2. 在该方向上的最后一个约束必须设置。

在您的情况下,您需要为链中的每个视图设置“layout_constraintBottom_toXXXXX”,并将最后一个视图的底部设置为父级。

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ade4ad">
    <TextView
        android:id="@+id/first"
        android:layout_width="90dp"
        android:layout_height="40dp"
        app:layout_marginBottom="10dp"
        app:layout_constraintBottom_toTopOf="@+id/second"
        android:background="#000"/>
    <TextView
        android:id="@+id/second"
        android:layout_width="90dp"
        android:layout_height="40dp"
        app:layout_marginBottom="10dp"
        app:layout_constraintBottom_toTopOf="@+id/third"
        android:background="#fff"/>
    <TextView
        android:id="@+id/third"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:background="#fff"
        app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>

此外,除非您想要"layout_marginTop"正常工作,否则不需要反向依赖。

明白了。简单来说:视图的约束只尊重视图自身定义的边距。 - Anticro

11

你可以使用这个技巧,在下方创建一个空间,并与父元素底部对齐

<Space
   android:id="@+id/space"
   android:layout_width="wrap_content"
   android:layout_height="80dp"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintStart_toStartOf="parent" />

并将您的视图对齐到空间的顶部 app:layout_constraintBottom_toTopOf = "@+id / space" 就像这样

<TextView
    android:id="@+id/howNext"
    style="@style/white_action_btn_no_border"
    android:layout_width="344dp"
    android:layout_height="60dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="@string/got_it_next"
    app:layout_constraintBottom_toTopOf="@+id/space"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

-2
这不是LinearLayoutRelativeLayout,而是ConstraintLayout,所以您必须为相关布局提供LeftRightBottomTop Constraint。在您的情况下,您必须先将TextViewBottom_Top约束与第二个TextViewTop约束相连,这样您就可以在两个TextView之间获得边距。
您的布局应该像下面这样。
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ade4ad">
    <TextView
        android:id="@+id/first"
        android:layout_width="90dp"
        android:layout_height="40dp" 
        android:background="#000"
        app:layout_constraintTop_toTopOf="parent" 
        app:layout_constraintLeft_toLeftOf="parent" />
    <TextView
        android:id="@+id/second"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:background="#fff"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@+id/first" 
        app:layout_constraintLeft_toLeftOf="@+id/first" 
        app:layout_constraintRight_toRightOf="@+id/first" />
</android.support.constraint.ConstraintLayout>

2
你的解决方案没有解释第一个视图上的android:layout_marginBottom="10dp"为什么不起作用(即使使用了你的代码)。 - Ron____

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接