是否有任何理由为什么以下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>
为了
android:layout_marginBottom="20dp"
工作得很好,你应该使用
app:layout_constraintBottom_toBottomOf="parent"
布局顶部/底部边距仅适用于:
在您的情况下,您需要为链中的每个视图设置“layout_constraintBottom_toXXXXX”,并将最后一个视图设置为bottom到parent。
<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”有效之外,不需要反向依赖。
你可以使用这个技巧,创建一个空格波纹,与父底部对齐
<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" />
并在空间应用程序顶部对齐您的视图: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" />
这不是LinearLayout
或RelativeLayout
,它的ConstraintLayout
所以你必须给Left
,Right
,Bottom
,Top
Constraint
到相关布局,在你的情况下你必须给TextView
第一Bottom_Top
约束到TextView
第二。所以你可以获得两个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>