{"id":732,"date":"2023-02-26T15:41:26","date_gmt":"2023-02-26T07:41:26","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=732"},"modified":"2023-04-29T17:09:17","modified_gmt":"2023-04-29T09:09:17","slug":"how-to-set-negative-margin-in-constraintlayout","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/how-to-set-negative-margin-in-constraintlayout\/","title":{"rendered":"\u5982\u4f55\u5728 ConstraintLayout \u4e2d\u8bbe\u7f6e\u8d1f\u503c\u7684 Margin"},"content":{"rendered":"<p>\u5047\u5982\u6709\u73b0\u5728\u8fd9\u6837\u4e00\u4e2a\u9700\u6c42\uff0c\u4e24\u4e2a\u63a7\u4ef6\uff1a\u4e00\u4e2a ImageView \u548c \u4e00\u4e2a TextView<br \/>\n\u4f4d\u7f6e\u8981\u6c42\uff1ay \u8f74\u65b9\u5411\uff0cTextView \u5728 ImageView \u7684\u5e95\u7ebf\u4e4b\u4e0a 20dp\uff1bx \u8f74\u65b9\u5411\uff0cTextView \u5728 ImageVIew \u7684\u53f3\u8fb9\u7ebf\u7684\u5de6\u8fb9 20dp\u3002<\/p>\n<p>\u5982\u679c\u4f7f\u7528\u4f20\u7edf\u7684 RelativeLayout\uff0c\u5f88\u7b80\u5355\uff0c\u8bbe\u7f6e ImageView \u548c TextView \u7684\u76f8\u5bf9\u4f4d\u7f6e\u4e4b\u540e\uff0c\u8bbe\u7f6e TextView \u7684 <code>android:layout_marginStart<\/code> \u548c <code>android:layout_marginTop<\/code> \u4e3a -20 \u5373\u53ef\u8fbe\u5230\u4e0a\u8ff0\u9700\u6c42\u3002<\/p>\n<p><!-- more --><\/p>\n<blockquote>\n<ul>\n<li>\u5728 RelativeLayout \u5185\u90e8\u7684\u63a7\u4ef6\uff0c\u5b83\u7684 Margin \u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u8d1f\u6570\uff0c\u5e76\u4e14\u662f\u8d77\u4f5c\u7528\u7684<\/li>\n<li>\u5728 ConstraintLayout \u5185\u90e8\u7684\u63a7\u4ef6\uff0c\u5b83\u7684 Margin \u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u8d1f\u6570\uff0c\u4f46\u662f\u4e0d\u8d77\u4f5c\u7528<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u5982\u679c\u4f7f\u7528 ConstraintLayout \u7684\uff0c\u5c31\u4e0d\u80fd\u901a\u8fc7\u8bbe\u7f6e TextView \u63a7\u4ef6\u7684 <code>android:layout_marginStart<\/code> \u548c <code>android:layout_marginTop<\/code> \u4e3a -20 \u8fbe\u5230\u4e0a\u8ff0\u9700\u6c42\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u6362\u79cd\u601d\u8def\uff0c\u5728 SDK \u5185\u63d0\u4f9b\u4e86\u53e6\u4e00\u4e2a\u63a7\u4ef6 <code>android.widget.Space<\/code>\uff0c\u5176\u5b98\u65b9\u8bf4\u660e\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<blockquote>\n<p>Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.\uff08Space\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684View\u5b50\u7c7b\uff0c\u53ef\u7528\u4e8e\u5728\u901a\u7528\u5e03\u5c40\u4e2d\u521b\u5efa\u7ec4\u4ef6\u4e4b\u95f4\u7684\u95f4\u9699\uff09<\/p>\n<\/blockquote>\n<p>\u4f7f\u7528 ConstraintLayout \u548c Space \u5b9e\u73b0\u4e0a\u56fe\u6240\u793a\u7684 xml \u5e03\u5c40\u6587\u4ef6\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;android.support.constraint.ConstraintLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    xmlns:app=&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot;\n    xmlns:tools=&quot;http:\/\/schemas.android.com\/tools&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;\n    tools:context=&quot;.MainActivity&quot;&gt;\n\n    &lt;ImageView\n        android:id=&quot;@+id\/iv&quot;\n        android:layout_width=&quot;wrap_content&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:layout_marginTop=&quot;100dp&quot;\n        android:contentDescription=&quot;@null&quot;\n        android:src=&quot;@mipmap\/ic_launcher&quot;\n        app:layout_constraintEnd_toEndOf=&quot;parent&quot;\n        app:layout_constraintStart_toStartOf=&quot;parent&quot;\n        app:layout_constraintTop_toTopOf=&quot;parent&quot; \/&gt;\n\n    &lt;android.widget.Space\n        android:id=&quot;@+id\/space&quot;\n        android:layout_width=&quot;0dp&quot;\n        android:layout_height=&quot;0dp&quot;\n        android:layout_marginBottom=&quot;20dp&quot;\n        android:layout_marginEnd=&quot;20dp&quot;\n        android:background=&quot;@android:color\/holo_blue_bright&quot;\n        app:layout_constraintBottom_toBottomOf=&quot;@+id\/iv&quot;\n        app:layout_constraintEnd_toEndOf=&quot;@+id\/iv&quot; \/&gt;\n\n    &lt;TextView\n        android:layout_width=&quot;wrap_content&quot;\n        android:layout_height=&quot;48dp&quot;\n        android:background=&quot;@android:color\/holo_blue_bright&quot;\n        android:gravity=&quot;center&quot;\n        android:text=&quot;Hello World!&quot;\n        android:textColor=&quot;@android:color\/white&quot;\n        app:layout_constraintStart_toEndOf=&quot;@+id\/space&quot;\n        app:layout_constraintTop_toBottomOf=&quot;@+id\/space&quot; \/&gt;\n&lt;\/android.support.constraint.ConstraintLayout&gt;<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\uff1aSpace \u63a7\u4ef6\u4e5f\u53ef\u4f7f\u7528\u666e\u901a View \u63a7\u4ef6\u4ee3\u66ff<\/p>\n<\/blockquote>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/stackoverflow.com\/questions\/42984909\/how-to-achieve-overlap-negative-margin-on-constraint-layout\" title=\"How to achieve overlap\/negative margin on Constraint Layout?\">How to achieve overlap\/negative margin on Constraint Layout?<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5047\u5982\u6709\u73b0\u5728\u8fd9\u6837\u4e00\u4e2a\u9700\u6c42\uff0c\u4e24\u4e2a\u63a7\u4ef6\uff1a\u4e00\u4e2a ImageView \u548c \u4e00\u4e2a TextView \u4f4d\u7f6e\u8981\u6c42\uff1ay \u8f74\u65b9\u5411\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[217],"class_list":["post-732","post","type-post","status-publish","format-standard","hentry","category-android-ui","tag-constraintlayout"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=732"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/732\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}