android新特性,constraintlayout實操

參考資料: http://blog.csdn.net/guolin_blog/article/details/53122387html

                  https://developer.android.google.cn/reference/android/support/constraint/ConstraintLayout.htmlandroid

1、constraintlayout簡介app

      ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在去年的I/O大會上重點宣傳的一個功能。在傳統的Android開發當中,界面基本都是靠編寫XML代碼完成的。雖然Android Studio也支持可視化的方式來編寫界面,可是操做起來並不方便。ConstraintLayout就是爲了解決這一現狀而出現的,能夠方便的進行可視化的編寫界面。ide

 

    ConstraintLayout還有一個優勢,它能夠有效地解決佈局嵌套過多的問題。ConstraintLayout則是使用約束的方式,來指定各個控件的位置和關係的。它有點相似於RelativeLayout,但遠比RelativeLayout要更強大。佈局

 

2、約束類型ui

      2.1如下是目前能使用的約束類型:this


##Relative positioning
如下是相對約束的類型: layout_constraintLeft_toLeftOf layout_constraintLeft_toRightOf layout_constraintRight_toLeftOf layout_constraintRight_toRightOf layout_constraintTop_toTopOf layout_constraintTop_toBottomOf layout_constraintBottom_toTopOf layout_constraintBottom_toBottomOf layout_constraintBaseline_toBaselineOf layout_constraintStart_toEndOf layout_constraintStart_toStartOf layout_constraintEnd_toStartOf layout_constraintEnd_toEndOf

 

 ##Margins:  The usual layout margin attributes can be used to this effect: android:layout_marginStart android:layout_marginEnd android:layout_marginLeft android:layout_marginTop android:layout_marginRight android:layout_marginBottom Note that a margin can only be positive or equals to zero, and takes a Dimension.

 

##center position
Bias
  • layout_constraintHorizontal_bias
  • layout_constraintVertical_bias
<android.support.constraint.ConstraintLayout ...>
             <Button android:id="@+id/button" ... app:layout_constraintHorizontal_bias="0.3" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent/>
         </>

 

 

   

      2.2常見操做google

  • 創建項目:添加constraintlayout依賴
  • other layout convert to constraintlayout
  •  刪除約束
  • 改變組件的寬度和高度的模式
 ##創建項目:添加constraintlayout依賴 implementation 'com.android.support.constraint:constraint-layout:1.0.2'

 

   ##other layout convert to constraintlayoutspa

 

 

 

##刪除約束方法

下圖中,1爲單個約束刪除,2爲刪除單個組件的全部約束,3爲刪除全部組件的全部約束

 

 

 

   ##改變組件的寬度和高度的模式.net

位於Inspector最中間的那個正方形區域,它是用來控制控件大小的。一共有三種模式可選,每種模式都使用了一種不一樣的符號表示,點擊符號便可進行切換。

 
表示wrap content,這個咱們很熟悉了,不須要進行什麼解釋。
 
 表示固定值,也就是給控件指定了一個固定的長度或者寬度值。
 
 表示any size,它有點相似於match parent,但和match parent並不同,是屬於ConstraintLayout中特有的一種大小控制方式,下面咱們來重點講解一下。

 

3、guideline

    虛擬組件,輔助頁面佈局。

  •      Horizontal guidelines
  •     Vertical guidelines
##guideline

 

 

 

4、自動添加約束

  •         Autoconnect:打開該開關按鈕,爲添加的單個組件自動添加約束
  •        infer Constraints:點擊該按鈕,爲頁面全部組件自動添加約束
autoconnect

 

 infer Constraints

相關文章
相關標籤/搜索