ConstraintLayout筆記

居中

底部覆蓋一點(diǎn)的居中效果

        app:layout_constraintTop_toBottomOf="@+id/imageview"
        app:layout_constraintBottom_toBottomOf="@+id/imageview"
        app:layout_constraintStart_toStartOf="@+id/imageview"
        app:layout_constraintEnd_toEndOf="@+id/imageview"

權(quán)重

layout_constraintHorizontal_weight需要配合layout_width0dp(math constraint) 時使用

 <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="200dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view2"
        android:background="#666"
        />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="200dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintEnd_toStartOf="@id/view3"
        app:layout_constraintStart_toEndOf="@id/view1"
        app:layout_constraintTop_toTopOf="parent"
        android:background="#999"
        />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="200dp"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/view2"
        app:layout_constraintTop_toTopOf="parent"
        android:background="#e1e1e1"
        />

角度

constraintCircleAngle 是順時針的角度
constraintCircleRadius 距離
constraintCircle 約束控件的id

   app:layout_constraintCircleAngle="45"
   app:layout_constraintCircleRadius="150dp"
   app:layout_constraintCircle="@+id/centerimag"

constrainedWidth 約束寬度

文字顯示在圖片下方, 文字的寬度不可以超過圖片
constrainedWidth 約束寬度

 <ImageView
        android:id="@+id/centerimag"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"
        android:background="#e1e1e1"
        app:layout_constraintTop_toBottomOf="@+id/centerimag"
        app:layout_constraintStart_toStartOf="@+id/centerimag"
        app:layout_constraintEnd_toEndOf="@+id/centerimag"
        android:layout_marginTop="20dp"
        app:layout_constrainedWidth="true"
        />

constraintHorizontal_bias用法

constraintHorizontal_bias的值 0靠左邊, 1靠右,0.5居中

<ImageView
        android:id="@+id/centerimag"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈哈哈哈哈哈哈哈哈哈哈"
        app:layout_constrainedWidth="true"
        android:background="#e1e1e1"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constrainRight_toLeftOf="@+id/centerimag"
        app:layout_constraintStart_toEndOf="@+id/centerimag"
        app:layout_constraintTop_toTopOf="parent"
        />  

goneMarginStart用法

goneMarginStart 當(dāng)前面的控件隱藏后, 加邊距

<TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈哈哈哈哈哈哈哈哈哈哈"
        android:background="#e1e1e1"
        android:visibility="gone"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <ImageView
        android:id="@+id/centerimag"
        android:layout_width="50dp"
        android:layout_height="50dp"
        app:layout_goneMarginStart="20dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintStart_toEndOf="@+id/textview"
        app:layout_constraintTop_toTopOf="parent"
        />

layout_constraintVertical_chainStyle 約束鏈用法

layout_constraintVertical_chainStyle 將兩個控件打包, 只能寫在第一個控件上

<ImageView
        app:layout_constraintVertical_chainStyle="packed"
        android:id="@+id/centerimag"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintBottom_toTopOf="@+id/textview"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_goneMarginStart="20dp"
        />

    <TextView
        android:id="@+id/textview"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#e1e1e1"
        android:text="哈哈哈哈哈哈哈哈哈哈哈哈哈"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/centerimag"
        app:layout_constraintStart_toStartOf="@+id/centerimag"
        app:layout_constraintTop_toBottomOf="@+id/centerimag" />

    <ImageView
        android:id="@+id/view1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view2"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/view2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view3"
        app:layout_constraintStart_toEndOf="@id/view1" />

    <ImageView
        android:id="@+id/view3"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/view2" />

constraintDimensionRatio寬高比例

需要配合寬或高為0dp使用

 app:layout_constraintDimensionRatio="1:1"

Guideline 輔助線使用

 <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2" />

    <ImageView
        android:id="@+id/view2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintEnd_toStartOf="@id/guideline"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈哈哈哈"
        app:layout_constraintLeft_toRightOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent" />

Group 一組控件操作

//對group設(shè)置gone屬性就能將constraint_referenced_ids里相關(guān)的id都隱藏
<androidx.constraintlayout.widget.Group
            android:id="@+id/group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:constraint_referenced_ids="tv1,tv2,tv3" />

Layer 一組控件一起操作移動

Barrier

 <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="end"
        app:constraint_referenced_ids="tv1,tv2"
        />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈哈哈哈"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageview1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintStart_toEndOf="@+id/tv1"
        app:layout_constraintTop_toTopOf="@+id/tv1" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈"
        android:layout_marginTop="30dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageview1"
        />

    <ImageView
        android:id="@+id/imageview2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintStart_toStartOf="@+id/imageview1"
        app:layout_constraintTop_toTopOf="@+id/tv2"
        />

ConstraintHelper 動畫復(fù)用

class CircularRevealHelper(context: Context, attrs:AttributeSet) : ConstraintHelper(context,attrs ) {

    /**
     * 所有的布局創(chuàng)建完成的回調(diào)
     */
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun updatePostLayout(container: ConstraintLayout) {
        super.updatePostLayout(container)

        referencedIds.forEach {
            val  view=container.getViewById(it)
            val radius:Int= hypot(view.width.toDouble(),view.height.toDouble()).toInt()
            ViewAnimationUtils.createCircularReveal(view,0,0,0f,0f)
                .setDuration(2000L)
                .start()
        }
    }
}
//xml中引用
    <com.example.kotlinstudy.view.CircularRevealHelper
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="imageview1,imageview2" />

Placeholder 占位符

  <androidx.constraintlayout.widget.Placeholder
        android:id="@+id/placeholder"
        android:layout_width="48dp"
        android:layout_height="48dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />
//搞幾個imageview設(shè)置上onClick
//代碼實(shí)現(xiàn)
fun onClick(view:View){
        findViewById<Placeholder>(R.id.placeholder).setContentId(view.id)
    }

控件位置改變 constraintSet

  fun onClick(view: View) {
        val constrainLayout: ConstraintLayout = view as ConstraintLayout
        val constraintSet: ConstraintSet = ConstraintSet().apply {
            clone(constrainLayout)
            connect(
                R.id.imageview1,
                ConstraintSet.BOTTOM,
                ConstraintSet.PARENT_ID,
                ConstraintSet.BOTTOM
            )
        }
        constraintSet.applyTo(constrainLayout)
    }

Folw 排列

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市弃衍,隨后出現(xiàn)的幾起案子呀非,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件岸裙,死亡現(xiàn)場離奇詭異猖败,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)降允,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進(jìn)店門恩闻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人剧董,你說我怎么就攤上這事幢尚。” “怎么了翅楼?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵尉剩,是天一觀的道長。 經(jīng)常有香客問我毅臊,道長理茎,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任管嬉,我火速辦了婚禮皂林,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蚯撩。我一直安慰自己础倍,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布求厕。 她就那樣靜靜地躺著著隆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪呀癣。 梳的紋絲不亂的頭發(fā)上美浦,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天,我揣著相機(jī)與錄音项栏,去河邊找鬼浦辨。 笑死,一個胖子當(dāng)著我的面吹牛沼沈,可吹牛的內(nèi)容都是我干的流酬。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼列另,長吁一口氣:“原來是場噩夢啊……” “哼芽腾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起页衙,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤摊滔,失蹤者是張志新(化名)和其女友劉穎阴绢,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體艰躺,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡呻袭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了腺兴。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片左电。...
    茶點(diǎn)故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖页响,靈堂內(nèi)的尸體忽然破棺而出篓足,到底是詐尸還是另有隱情,我是刑警寧澤拘泞,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布纷纫,位于F島的核電站,受9級特大地震影響陪腌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜烟瞧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一诗鸭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧参滴,春花似錦强岸、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至暴心,卻和暖如春妓盲,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背专普。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工悯衬, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人檀夹。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓筋粗,卻偏偏與公主長得像,于是被迫代替她去往敵國和親炸渡。 傳聞我的和親對象是個殘疾皇子娜亿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容