app:layout_marginBottom与android约束布局不兼容

Ron*_*___ 21 android margin android-constraintlayout

是否有任何理由为什么以下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>
Run Code Online (Sandbox Code Playgroud)

Bey*_*yaz 27

为了

android:layout_marginBottom="20dp" 
Run Code Online (Sandbox Code Playgroud)

工作得很好,你应该使用

app:layout_constraintBottom_toBottomOf="parent"
Run Code Online (Sandbox Code Playgroud)

  • 如果您希望遵守顶部和底部边距,则必须对所有视图的顶部和底部应用约束。左右约束和左右边距也是如此。 (5认同)

Jac*_*ack 10

你可以使用这个技巧,在下面创建一个空格,与父底部对齐

<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" />
Run Code Online (Sandbox Code Playgroud)

并将您的视图对齐在空间应用程序的顶部: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" />
Run Code Online (Sandbox Code Playgroud)


sup*_*ser 5

布局顶部/底部边距仅在以下情况下有效:

  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>
Run Code Online (Sandbox Code Playgroud)

此外,不需要反向依赖,除非您希望“ layout_marginTop”起作用。


Meh*_*ria -2

这不是LinearLayoutor RelativeLayoutConstraintLayout因此您必须将Left, Right, Bottom,Top Constraint赋予相关布局,在您的情况下,您必须将TextView第一个Bottom_Top约束赋予TextView第二个。这样你就可以获得 Two 之间的 Margin 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>
Run Code Online (Sandbox Code Playgroud)

  • 您的解决方案无法解释为什么 android:layout_marginBottom="10dp" 在第一个视图上不起作用(也适用于您的代码)。 (2认同)