兩個控件:一個 ImageView 和 一個 TextView
位置要求:y 軸方向耘分,TextView 在 ImageView 的底線之上 20dp点弯;x 軸方向裆馒,TextView 在 ImageVIew 的右邊線的左邊 20dp蹦掐。
如果使用傳統(tǒng)的 RelativeLayout 的話,很簡單户辞,設(shè)置 ImageView 和 TextView 的相對位置之后泌类,設(shè)置 TextView 的 android:layout_marginStart
和 android:layout_marginTop
為 -20 便可以達(dá)到上述需求的效果了,上圖所對應(yīng)的 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 內(nèi)部的控件底燎,它的 Margin 可以設(shè)置為負(fù)數(shù)刃榨,并且是起作用的
- 在 ConstraintLayout 內(nèi)部的控件,它的 Margin 可以設(shè)置為負(fù)數(shù)双仍,但是不起作用
如果使用 ConstraintLayout 的枢希,就不能通過設(shè)置 TextView 控件的 android:layout_marginStart
和 android:layout_marginTop
為 -20 達(dá)到上述需求了。
那我們可以換種思路朱沃,在 SDK 內(nèi)提供了另一個控件 android.widget.Space
苞轿,其官方說明如下所示:
Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.(Space是一個輕量級的View子類茅诱,可用于在通用布局中創(chuàng)建組件之間的間隙。)
使用 ConstraintLayout 和 Space 實現(xiàn)上圖所示的 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 版本開始已經(jīng)被廢棄了瑟俭,所以推薦使用android.widget.Space
Space1.png
參考資料:
How to achieve overlap/negative margin on Constraint Layout?