android 布局之ConstraintLayout

其實(shí)ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在2016年的I/O大會(huì)上重點(diǎn)宣傳的一個(gè)功能。是為了android可視化編輯而做的努力。android studio 的可視化編輯個(gè)人不推薦使用,不過(guò)ConstraintLayout布局的使用還是有必要了解的。
1,要想使用ConstraintLayout需要在app的build.gradle里面引入:

  compile 'com.android.support.constraint:constraint-layout:1.0.2'

2,首先看一個(gè)簡(jiǎ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="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="第二個(gè)控件"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tv1"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="第三個(gè)控件"
    app:layout_constraintBottom_toBottomOf="parent"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="第四個(gè)控件"
    app:layout_constraintLeft_toRightOf="@+id/tv1"
    android:layout_marginLeft="10dp"
    />
 </android.support.constraint.ConstraintLayout>
image.png

各位看客官不難看出,xml中四個(gè)簡(jiǎn)單的Textview分布位置,以id為tv的第一個(gè)控件為基準(zhǔn),第二個(gè)控件在第一個(gè)控件的下方,且都在整個(gè)布局的左邊;第三個(gè)控件在整個(gè)父布局的左下方;第四個(gè)控件在第一個(gè)控件的右邊。
仔細(xì)觀察,每個(gè)Textview都有類似的屬性:
比如第一個(gè)控件的:
app:layout_constraintTop_toTopOf="parent"
第二個(gè)控件的
app:layout_constraintTop_toBottomOf="@+id/tv1"
字面意思就是:
該控件的某個(gè)邊和某個(gè)控件的某個(gè)邊對(duì)齊。
比如,第一個(gè)控件是該控件的上部和父布局的上部對(duì)齊,自然就使得第一個(gè)控件處于左上方;第二個(gè)控件的頂部和第一個(gè)控件即id=tv1的控件的底部對(duì)齊,自然第二個(gè) 控件就會(huì)位于第一個(gè)控件的正下方。其他舉一反三即可。

現(xiàn)在我們規(guī)定一個(gè)布局再次試驗(yàn)一下,搞個(gè)最常見(jiàn)的布局。

image.png

代碼如下:

<?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="任務(wù)"
 <!-- 該控件的頂部和父布局的頂部對(duì)齊 !-->
    app:layout_constraintTop_toTopOf="parent"
 />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="已完成"
    <!-- 該控件的右邊和父布局的左邊對(duì)齊 !-->
    app:layout_constraintRight_toLeftOf="parent"
    android:layout_marginRight="10dp"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="劉剛"
 <!-- 該控件的頂部和tv1的底部對(duì)其齊!-->
    app:layout_constraintTop_toBottomOf="@+id/tv1"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="完成時(shí)間"
   <!-- 該控件的右邊和父布局的左邊對(duì)齊 !-->
    app:layout_constraintRight_toLeftOf="parent"
 <!-- 該控件的頂部和tv1的底部對(duì)齊 !-->
    app:layout_constraintTop_toBottomOf="@+id/tv1"
    android:layout_marginRight="10dp"
    />
</android.support.constraint.ConstraintLayout>
image.png

未完待續(xù)...還有其他常用屬性。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容