约束布局的 child 边距
Constraint Layout's child margins
我正在尝试在 constraintlayout.But 中使用两个视图,一旦分配了边距,child views.Any 上的未定义行为将不胜感激。
这是我的代码:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@drawable/ic_action_google_play"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="TextHere"
android:textSize="24sp"
app:layout_constraintLeft_toRightOf="@+id/img"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
在 constraintLayout 中使用填充而不是在每个子项中使用 layout_margin 来实现您的要求。
将您的代码更改为以下代码:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_google_play"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextHere"
android:textSize="24sp"
app:layout_constraintLeft_toRightOf="@+id/img"
app:layout_constraintTop_toTopOf="parent" />
this link 将帮助您区分 padding 和 margin。
在 Constraintlayout 中定义视图之间的边距时,请确保指定边距的位置:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/ic_launcher_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:text="TextHere"
android:textSize="24sp"
app:layout_constraintStart_toEndOf="@+id/img"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
结果
在 ConstraintLayout 中,要在任何一侧(左、上、右、下)应用边距,必须明确定义该侧的约束。如果您的情况是,您希望 ImageView 的每一侧都有“16dp”边距,因此您需要为该 ImageView 的每一侧声明约束。您在 XML 中写了以下内容:
<ImageView
android:layout_margin="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
像这样替换:
<ImageView
android:id="@+id/img"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
/>
要使 Right 和 Bottom 的边距起作用,您还需要添加以下内容:
app:layout_constraintRight_toLeftOf="@+id/txtNew"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
TextView 也一样。
我正在尝试在 constraintlayout.But 中使用两个视图,一旦分配了边距,child views.Any 上的未定义行为将不胜感激。
这是我的代码:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@drawable/ic_action_google_play"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="TextHere"
android:textSize="24sp"
app:layout_constraintLeft_toRightOf="@+id/img"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
在 constraintLayout 中使用填充而不是在每个子项中使用 layout_margin 来实现您的要求。
将您的代码更改为以下代码:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_google_play"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextHere"
android:textSize="24sp"
app:layout_constraintLeft_toRightOf="@+id/img"
app:layout_constraintTop_toTopOf="parent" />
this link 将帮助您区分 padding 和 margin。
在 Constraintlayout 中定义视图之间的边距时,请确保指定边距的位置:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/ic_launcher_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:text="TextHere"
android:textSize="24sp"
app:layout_constraintStart_toEndOf="@+id/img"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
结果
在 ConstraintLayout 中,要在任何一侧(左、上、右、下)应用边距,必须明确定义该侧的约束。如果您的情况是,您希望 ImageView 的每一侧都有“16dp”边距,因此您需要为该 ImageView 的每一侧声明约束。您在 XML 中写了以下内容:
<ImageView
android:layout_margin="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
像这样替换:
<ImageView
android:id="@+id/img"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
/>
要使 Right 和 Bottom 的边距起作用,您还需要添加以下内容:
app:layout_constraintRight_toLeftOf="@+id/txtNew"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
TextView 也一样。