Update
约束布局现在版本 2.1.0-alpha2 支持负边距。简单说明一下
android:layout_marginTop="-25dp"
负 25dp 边距。 (这仅在视图顶部受到约束时才有效。边距对约束布局如果边距一侧不受约束。)
澄清:下面的答案仍然有效,但我想澄清一些事情。原始解决方案将放置一个带有de facto相对于另一个视图的负偏移量将出现在如图所示的布局中。
另一种解决方案是使用翻译YAmir Khorsandi 建议的财产here。我更喜欢这种更简单的解决方案,但有一个警告:翻译发生布局后,因此受限于移位视图的视图将不会遵循翻译。
例如,以下 XML 显示两个文本视图紧邻图像下方。每个视图都从上到下受到紧邻其上方的视图的约束。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:tint="#388E3C"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_action_droid" />
<TextView
android:id="@+id/sayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say my name."
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView" />
<TextView
android:id="@+id/sayIt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say it."
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintEnd_toEndOf="@+id/sayName"
app:layout_constraintStart_toStartOf="@+id/sayName"
app:layout_constraintTop_toBottomOf="@id/sayName" />
</androidx.constraintlayout.widget.ConstraintLayout>
现在我们来翻译一下“说出我的名字”TextView up by 50dp
通过指定
android:translationY="-50dp"
这会产生以下结果:
《说出我的名字》TextView已按预期向上移动,但“说出来”TextView并没有像我们预期的那样跟进。这是因为翻译发生了布局后。尽管视图在布局后移动,但仍然可以在新位置使其可单击。
所以,IMO,一起去吧翻译X and 翻译Y对于负边距约束布局如果上述警告不影响您的布局;否则,请与space小部件如下所述。
另一个警告:正如 Salam El-Banna 在对另一个答案的评论中所述,翻译X对于 RTL 布局来说,这不是一个好的解决方案,因为无论布局的 RTL 或 LTR 性质如何,转换的符号将决定移位的方向(左/右)。
原答案
尽管似乎不支持负边距ConstraintLayout
,有一种方法可以使用可用且受支持的工具来实现该效果。这是图像标题重叠的图像22dp
从图像底部 - 实际上是-22dp
margin:
这是通过使用Space小部件的底部边距等于您想要的偏移量。这Space
然后小部件的底部被限制在ImageView
。现在您需要做的就是约束顶部TextView
图像标题位于底部Space
小部件。这TextView
将位于底部Space
视图忽略设置的边距。
以下是实现此效果的 XML。我会注意到我使用Space
因为它很轻并且适合这种类型的使用,但我可以使用另一种类型View
并使其不可见。 (不过,您可能需要进行调整。)您还可以定义一个View
具有零边距和所需的插入边距的高度,并约束顶部TextView
到插图的顶部View
.
另一种方法是覆盖TextView
在上面ImageView
通过对齐顶部/底部/左/右并对边距/填充进行适当的调整。下面演示的方法的好处是无需大量计算即可创建负裕度。这就是说,有几种方法可以解决这个问题。
Update:有关此技术的快速讨论和演示,请参阅 Google Developers Medium博客文章.
负保证金ConstraintLayout
XML
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<android.support.v4.widget.Space
android:id="@+id/marginSpacer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="22dp"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintLeft_toLeftOf="@id/imageView"
app:layout_constraintRight_toRightOf="@id/imageView" />
<TextView
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say my name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/marginSpacer" />
</android.support.constraint.ConstraintLayout>