如何在 ConstraintLayout 中设置负值的 Margin

假如有现在这样一个需求,如下图所示:

两个控件:一个 ImageView 和 一个 TextView
位置要求:y 轴方向,TextView 在 ImageView 的底线之上 20dp;x 轴方向,TextView 在 ImageVIew 的右边线的左边 20dp。

如果使用传统的 RelativeLayout 的话,很简单,设置 ImageView 和 TextView 的相对位置之后,设置 TextView 的 android:layout_marginStartandroid:layout_marginTop-20 便可以达到上述需求的效果了,上图所对应的 xml 布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:contentDescription="@null"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_below="@+id/iv"
        android:layout_marginStart="-20dp"
        android:layout_marginTop="-20dp"
        android:layout_toEndOf="@+id/iv"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center"
        android:text="Hello World!"
        android:textColor="@android:color/white" />
</RelativeLayout>
  • 在 RelativeLayout 内部的控件,它的 Margin 可以设置为负数,并且是起作用的
  • 在 ConstraintLayout 内部的控件,它的 Margin 可以设置为负数,但是不起作用

如果使用 ConstraintLayout 的,就不能通过设置 TextView 控件的 android:layout_marginStartandroid:layout_marginTop-20 达到上述需求了。

那我们可以换种思路,在 SDK 内提供了另一个控件 android.widget.Space,其官方说明如下所示:

Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.(Space是一个轻量级的View子类,可用于在通用布局中创建组件之间的间隙。)

使用 ConstraintLayout 和 Space 实现上图所示的 xml 布局文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:contentDescription="@null"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.widget.Space
        android:id="@+id/space"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="20dp"
        android:layout_marginEnd="20dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toBottomOf="@+id/iv"
        app:layout_constraintEnd_toEndOf="@+id/iv" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center"
        android:text="Hello World!"
        android:textColor="@android:color/white"
        app:layout_constraintStart_toEndOf="@+id/space"
        app:layout_constraintTop_toBottomOf="@+id/space" />
</android.support.constraint.ConstraintLayout>

note:在 support.v4 中也有一个 Space 的控件 android.support.v4.widget.Space,但是 v4 中的 Space 控件从 27.1.0 版本开始已经被废弃了,所以推荐使用 android.widget.Space

 参考资料
How to achieve overlap/negative margin on Constraint Layout?

 



作者:lijiankun24
链接:https://www.jianshu.com/p/a71f6511e477
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值