android constraintLayout设置margin为负数

效果图
代码如下,基本上就是利用space

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/id_constraint_layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        >

        <ImageView
            android:id="@+id/id_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher_round"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            />


        <Space
            android:id="@+id/id_space"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="60dp"
            android:background="@color/colorAccent"
            app:layout_constraintBottom_toBottomOf="@id/id_image"
            app:layout_constraintEnd_toEndOf="@id/id_image"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="小欢喜呀呀呀呀呀"
            android:textColor="#ffffff"
            android:background="@color/colorAccent"
            app:layout_constraintTop_toBottomOf="@id/id_space"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/id_constraint_layout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/id_constraint_layout1"
        >


        <Space
            android:id="@+id/id_space_margin_right"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginRight="50dp"
            app:layout_constraintRight_toRightOf="@id/id_image2"
            app:layout_constraintTop_toTopOf="@id/id_image2"
            />

        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@color/colorAccent"
            android:text="嘿嘿嘿--哈哈哈"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:gravity="center"
            app:layout_constraintRight_toLeftOf="@id/id_space_margin_right"
            app:layout_constraintTop_toTopOf="@id/id_image2"
            app:layout_constraintBottom_toBottomOf="@id/id_image2"
            />

        <ImageView
            android:id="@+id/id_image2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher_round"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />


    </androidx.constraintlayout.widget.ConstraintLayout>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_constraintTop_toBottomOf="@id/id_constraint_layout2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        >

        <Space
            android:id="@+id/id_space_margin_left"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginLeft="50dp"
            app:layout_constraintLeft_toLeftOf="@id/id_image3"
            app:layout_constraintTop_toTopOf="@id/id_image3"
            />

        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@color/colorAccent"
            android:text="嘿嘿嘿--哈哈哈"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:gravity="center"
            app:layout_constraintLeft_toRightOf="@id/id_space_margin_left"
            app:layout_constraintTop_toTopOf="@id/id_image3"
            app:layout_constraintBottom_toBottomOf="@id/id_image3"
            />

        <ImageView
            android:id="@+id/id_image3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher_round"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

完事

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`ConstraintLayout` 是Android中一个非常灵活和强大的布局管理器,它允许开发者创建复杂的布局结构,而不需要嵌套多个布局层次。在`ConstraintLayout`中,可以通过设置`dp`(设备独立像素)来控制组件的尺寸和位置,以此来适应不同屏幕密度(DPI)的设备。 在`ConstraintLayout`中设置尺寸和位置通常使用`layout_width`、`layout_height`、`layout_margin`等属性,并且可以为它们赋予`dp`值。例如: ```xml <androidx.constraintlayout.widget.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"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="16dp" android:layout_marginStart="16dp"/> <!-- 其他组件 --> </androidx.constraintlayout.widget.ConstraintLayout> ``` 在上面的XML代码中,`layout_marginTop`和`layout_marginStart`属性被用来设置组件距离父布局边缘的距离,这里使用的是`dp`作为单位。 对于响应不同DPI的设备,通常需要在资源文件中提供不同尺寸的图片资源和布局文件,系统会根据设备的屏幕密度自动选择合适的资源。开发者可以为不同的屏幕密度创建不同的`layout`文件夹,如`layout`, `layout-hdpi`, `layout-xhdpi`, `layout-xxhdpi`等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值