ConstraintLayout 布局设置margin负值无效解决办法

这篇博客探讨了在Android开发中,如何利用ConstraintLayout的Space组件来模拟负边距的效果,当直接设置margin为负值无法正常工作时,通过Space的布局约束可以达到类似目的。示例代码展示了如何设置Space的margin属性来实现相对于其他控件的负偏移。
摘要由CSDN通过智能技术生成

ConstraintLayout 设置 margin 为负值的时候,并不能像其他布局,如相对布局那样使用(TMD)不知道为什么,用什么辅助实现?

Space 对,用这个

来,撸码
<layout 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">
	
	<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layoutRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/gg_room_bg">
		
		<View
            android:id="@+id/viewRoomVipUsers"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="215dp"
            app:layout_constraintTop_toBottomOf="@+id/toolbar" />
		
		<!-- 相对上面的控件 top 负25dp、 left 负 15dp -->
		<Space
            android:id="@+id/marginSpacer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="25dp"
            android:layout_marginStart="15dp"
            app:layout_constraintBottom_toBottomOf="@+id/viewRoomVipUsers"
            app:layout_constraintLeft_toLeftOf="@id/viewRoomVipUsers"
            app:layout_constraintRight_toRightOf="@id/viewRoomVipUsers" />
		
		<View
            android:id="@+id/enterLevelRoom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/marginSpacer"
            android:visibility="gone"
            tools:ignore="MissingConstraints" />
	</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
ok,可以了,去把自己的嵌套去掉吧!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值