這期內容當中小編將會給大家帶來有關android中怎么使用ConstraintLayout 布局,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1,要想使用ConstraintLayout需要在app的build.gradle里面引入:
compile 'com.android.support.constraint:constraint-layout:1.0.2'
2,首先看一個簡單的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="com.app.qichun.hellowrod.MainActivity"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintTop_toTopOf="parent" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二個控件" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/tv1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三個控件" app:layout_constraintBottom_toBottomOf="parent" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第四個控件" app:layout_constraintLeft_toRightOf="@+id/tv1" android:layout_marginLeft="10dp" /> </android.support.constraint.ConstraintLayout>
各位看客官不難看出,xml中四個簡單的Textview分布位置,以id為tv的第一個控件為基準,第二個控件在第一個控件的下方,且都在整個布局的左邊;第三個控件在整個父布局的左下方;第四個控件在第一個控件的右邊。
仔細觀察,每個Textview都有類似的屬性:
比如第一個控件的:
app:layout_constraintTop_toTopOf="parent"
第二個控件的
app:layout_constraintTop_toBottomOf="@+id/tv1"
字面意思就是:
該控件的某個邊和某個控件的某個邊對齊。
比如,第一個控件是該控件的上部和父布局的上部對齊,自然就使得第一個控件處于左上方;第二個控件的頂部和第一個控件即id=tv1的控件的底部對齊,自然第二個 控件就會位于第一個控件的正下方。其他舉一反三即可。
現在我們規定一個布局再次試驗一下,搞個最常見的布局。
代碼如下:
<?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="com.app.qichun.hellowrod.MainActivity"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="任務" <!-- 該控件的頂部和父布局的頂部對齊 !--> app:layout_constraintTop_toTopOf="parent" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="已完成" <!-- 該控件的右邊和父布局的左邊對齊 !--> app:layout_constraintRight_toLeftOf="parent" android:layout_marginRight="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="劉剛" <!-- 該控件的頂部和tv1的底部對其齊!--> app:layout_constraintTop_toBottomOf="@+id/tv1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="完成時間" <!-- 該控件的右邊和父布局的左邊對齊 !--> app:layout_constraintRight_toLeftOf="parent" <!-- 該控件的頂部和tv1的底部對齊 !--> app:layout_constraintTop_toBottomOf="@+id/tv1" android:layout_marginRight="10dp" /> </android.support.constraint.ConstraintLayout>
上述就是小編為大家分享的android中怎么使用ConstraintLayout 布局了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。