constraintLayout的一些高級用法 布局一個16:9的圖片 以及GuideLine的使用


    <!-- "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" /> 

預覽:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM