Ответ 1
Для того, чтобы
android:layout_marginBottom="20dp"
работать хорошо, вы должны использовать
app:layout_constraintBottom_toBottomOf="parent"
Есть ли причина, почему следующий 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" для каждого представления в цепочке, а последнее представление установить снизу для родителя.
<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/пространство" вот так
<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" />
Не знаю, почему это не работает, на самом деле это ограничение компоновки ограничений.
Но вы можете добавить android:layout_marginTop="10dp"
и app:layout_goneMarginTop="0dp"
во второй TextView, он будет работать таким же образом.
Это не LinearLayout
или RelativeLayout
, его ConstraintLayout
, поэтому вам нужно дать Left
, Right
, Bottom
, Top
Constraint
Соответствующий макет, в вашем случае у вас есть чтобы дать TextView
сначала Bottom_Top
Ограничение на TextView
второе. поэтому вы можете получить 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>