国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

android 布局之ConstraintLayout的使用

2019-10-22 18:12:41
字體:
供稿:網(wǎng)友

其實android/255823.html">ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在2016年的I/O大會上重點宣傳的一個功能。是為了android可視化編輯而做的努力。android studio 的可視化編輯個人不推薦使用,不過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>

android,ConstraintLayout,布局

各位看客官不難看出,xml中四個簡單的Textview分布位置,以id為tv的第一個控件為基準,第二個控件在第一個控件的下方,且都在整個布局的左邊;第三個控件在整個父布局的左下方;第四個控件在第一個控件的右邊。

仔細觀察,每個Textview都有類似的屬性:

比如第一個控件的:

app:layout_constraintTop_toTopOf="parent"

第二個控件的

app:layout_constraintTop_toBottomOf="@+id/tv1"

字面意思就是:

該控件的某個邊和某個控件的某個邊對齊。

比如,第一個控件是該控件的上部和父布局的上部對齊,自然就使得第一個控件處于左上方;第二個控件的頂部和第一個控件即id=tv1的控件的底部對齊,自然第二個 控件就會位于第一個控件的正下方。其他舉一反三即可。
現(xiàn)在我們規(guī)定一個布局再次試驗一下,搞個最常見的布局。

android,ConstraintLayout,布局

代碼如下:

<?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ù)" <!-- 該控件的頂部和父布局的頂部對齊 !-->  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,布局

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 通化县| 鸡东县| 抚松县| 陇西县| 从江县| 霍邱县| 饶河县| 稷山县| 轮台县| 兴仁县| 灵璧县| 个旧市| 丰城市| 岳西县| 乐业县| 五寨县| 商南县| 胶州市| 丰原市| 固阳县| 临湘市| 自治县| 怀仁县| 东乡县| 平陆县| 含山县| 宝山区| 浦江县| 收藏| 永济市| 隆林| 通山县| 博爱县| 海口市| 仁寿县| 乐东| 册亨县| 乌拉特中旗| 钟山县| 南乐县| 河池市|