android -------- ConstraintLayout 寬高比和偏移量比(三)

前面的html

ConstraintLayout 介紹(一)android

ConstraintLayout 約束屬性(二)app

此博文主要講解:ide

app:layout_constraintHorizontal_bias
app:layout_constraintDimensionRatio佈局

 

 
1:app:layout_constraintDimensionRatio(寬高比/百分比佈局)google

這個屬性感受很是實用,按照比例來分配佈局spa

案例:.net

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <!--
    layout_constraintDimensionRatio(寬高比)
    若是想實現一個固定寬高比的頂部標題欄的話,能夠將寬和高設置爲 0dp,
    而後爲其設置 app:layout_constraintDimensionRatio 屬性,設定寬高比爲16:7-->
    <TextView
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        app:layout_constraintDimensionRatio="h,16:8"
        android:gravity="center"
        android:text="@string/app_name"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <!--底部  app:layout_constraintDimensionRatio="w,1:3"-->
    <TextView
        android:id="@+id/tv2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        app:layout_constraintDimensionRatio="w,1:3"
        android:gravity="center"
        android:text="stringapp_name"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />


    <!--剩餘佈局居中-->
    <TextView
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="@string/app_name"
        android:background="#ff0000"
        app:layout_constraintDimensionRatio="w,15:25"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv1"
        app:layout_constraintBottom_toTopOf="@+id/tv2"
        />

</android.support.constraint.ConstraintLayout>
app:layout_constraintDimensionRatio="h,16:8"  按照寬高的比例,也能夠
app:layout_constraintDimensionRatio="w,1:3"   按照高寬的比例

效果圖:

              

 

2:app:layout_constraintHorizontal_bias(偏移量比)

鏈條分爲水平鏈條和豎直鏈條兩種,分別用 layout_constraintHorizontal_chainStyle 和 layout_constraintVertical_chainStyle 兩個屬性來設置
屬性值有如下三種:3d

  • spread
  • spread_inside
  • packed

默認值爲 spreadcode

能夠經過下圖來理解各個參數值的含義

   
 

舉例(水平的)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <!--
    layout_constraintHorizontal_chainStyle
    app:layout_constraintHorizontal_bias(偏移量比)等
    -->

    <!--

         https://www.jianshu.com/p/b884b8c46584
        spread:中間居中,且包含一段空格,平均間隔,4個空格
        packed:中間居中,所有挨在一塊兒
        spread_inside: 左右貼邊緣,中間居中,中間兩個平分間隔
        -->


    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="#f5ec7e"
        android:text="Hello World!"
        android:gravity="center"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintRight_toLeftOf="@+id/tv2"
        />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="#55e4f4"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@+id/tv1"
        app:layout_constraintRight_toLeftOf="@+id/tv3"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="#f186ad"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@+id/tv2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />





    <!--layout_constraintHorizontal_bias  不設置就居中
        設置了則向水平偏移
        app:layout_constraintVertical_bias="0.5" 垂直中間
    -->
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/app_name"
        android:background="@color/colorAccent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.2"
        android:gravity="center"
        />

</android.support.constraint.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--垂直的比例佈局 layout_constraintVertical_chainStyle-->

    <TextView
        android:id="@+id/tv1"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:background="#f5ec7e"
        android:gravity="center"
        android:text="@string/app_name"
        app:layout_constraintBottom_toTopOf="@+id/tv2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread_inside"
        app:layout_constraintVertical_weight="2" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:background="#55e4f4"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintBottom_toTopOf="@+id/tv3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv1"
        app:layout_constraintVertical_chainStyle="spread_inside"
        app:layout_constraintVertical_weight="1" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:background="#f186ad"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv2"
        app:layout_constraintVertical_chainStyle="spread_inside"
        app:layout_constraintVertical_weight="1" />


</android.support.constraint.ConstraintLayout>

效果圖:

          

 

 

參考文檔: 

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

https://constraintlayout.com/basics/create_chains.html

https://www.jianshu.com/p/b884b8c46584

相關文章
相關標籤/搜索