欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android 布局之ConstraintLayout的使用

 更新時間:2018年04月21日 08:45:34   作者:蝦皮皮  
這篇文章主要介紹了android 布局之ConstraintLayout的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

其實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>


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

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

比如第一個控件的:

app:layout_constraintTop_toTopOf="parent"

第二個控件的

app:layout_constraintTop_toBottomOf="@+id/tv1"

字面意思就是:

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

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

代碼如下:

<?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>

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

相關(guān)文章

  • Android實現(xiàn)動態(tài)切換組件背景的方法

    Android實現(xiàn)動態(tài)切換組件背景的方法

    這篇文章主要介紹了Android實現(xiàn)動態(tài)切換組件背景的方法,需要的朋友可以參考下
    2014-07-07
  • Android獲取本機各種類型文件的方法

    Android獲取本機各種類型文件的方法

    這篇文章主要為大家詳細(xì)介紹了Android獲取本機各種類型文件的方法,包括音樂、視頻、圖片、文檔等,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android onbackpressed實現(xiàn)返回鍵的攔截和彈窗流程分析

    Android onbackpressed實現(xiàn)返回鍵的攔截和彈窗流程分析

    很多網(wǎng)友不明白如何在Android平臺上捕獲Back鍵的事件,Back鍵是手機上的后退鍵,一般的軟件不捕獲相關(guān)信息可能導(dǎo)致你的程序被切換到后臺,而回到桌面的尷尬情況,在Android上有兩種方法來獲取該按鈕的事件
    2023-01-01
  • flutter日期選擇器 flutter時間選擇器

    flutter日期選擇器 flutter時間選擇器

    這篇文章主要為大家詳細(xì)介紹了flutter日期選擇器,flutter時間選擇器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Android簡單實現(xiàn)自定義彈框(PopupWindow)

    Android簡單實現(xiàn)自定義彈框(PopupWindow)

    本文主要介紹了Android利用PopupWindow實現(xiàn)自定義彈框的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Android實現(xiàn)讀取NFC卡的編號

    Android實現(xiàn)讀取NFC卡的編號

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)讀取NFC卡的編號,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • BroadcastReceiver動態(tài)注冊案例詳解

    BroadcastReceiver動態(tài)注冊案例詳解

    這篇文章主要為大家詳細(xì)介紹了BroadcastReceiver動態(tài)注冊案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android控件CardView實現(xiàn)卡片布局

    Android控件CardView實現(xiàn)卡片布局

    這篇文章主要為大家詳細(xì)介紹了Android控件CardView實現(xiàn)卡片布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Jetpack?Compose?的新型架構(gòu)?MVI使用詳解

    Jetpack?Compose?的新型架構(gòu)?MVI使用詳解

    這篇文章主要介紹了Jetpack?Compose?的新型架構(gòu)?MVI使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Android?高德地圖POI定位地址搜索功能

    Android?高德地圖POI定位地址搜索功能

    這篇文章主要介紹了Android?高德地圖POI定位地址搜索功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-02-02

最新評論