<!-- "W,9:16" 同樣的效果 --> <ImageView android:layout_width="0dp" android:layout_height="0dp" android:scaleType="centerCrop" android:src="@mipmap/icon" app:layout_constraintDimensionRatio="H,16:9" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/>
guideline 使用
<android.support.constraint.Guideline
android:id="@+id/college_guideline_109dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="109dp" />
設置guideLine的位置有兩種方式:""
Guideline
Guideline
是只能用在ConstraintLayout布局里面的一個工具類,用於輔助布局,類似為輔助線,可以設置android:orientation
屬性來確定是橫向的還是縱向的。
- 當設置為
vertical
的時候,Guideline
的寬度為0,高度是parent
也就是ConstraintLayout的高度 - 同樣設置為
horizontal
的時候,高度為0,寬度是parent
的寬度
重要的是Guideline是不會顯示到界面上的,默認是GONE
的。
Guideline
還有三個重要的屬性,每個Guideline
只能指定其中一個:
layout_constraintGuide_begin
,指定左側或頂部的固定距離,如100dp,在距離左側或者頂部100dp的位置會出現一條輔助線layout_constraintGuide_end
,指定右側或底部的固定距離,如30dp,在距離右側或底部30dp的位置會出現一條輔助線layout_constraintGuide_percent
,指定在父控件中的寬度或高度的百分比,如0.8,表示距離頂部或者左側的80%的距離。
通過一個栗子便能理解:
<android.support.constraint.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/guidelineBegin" app:layout_constraintGuide_begin="100dp" android:orientation="vertical"/> <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" app:layout_constraintLeft_toLeftOf="@+id/guidelineBegin" android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent" /> <android.support.constraint.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/guidelineEnd" app:layout_constraintGuide_end="100dp" android:orientation="vertical"/> <Button android:text="Button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonEnd" app:layout_constraintRight_toLeftOf="@+id/guidelineEnd" android:layout_marginTop="48dp" app:layout_constraintTop_toTopOf="parent" /> <android.support.constraint.Guideline android:id="@+id/guidelinePercent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.8" /> <Button android:text="Button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonPercent" app:layout_constraintLeft_toLeftOf="@+id/guidelinePercent" android:layout_marginTop="96dp" app:layout_constraintTop_toTopOf="parent" />
預覽:
