ConstraintLayout 爬坑記錄

新項目中佈局全部用的ConstraintLayout 方便是方便但是有些東西還要慢慢摸索纔行

比如ConstraintLayout 如何在一行上的控件進行等比平分 類似於 LinearLayout 的 layout_weight

自己摸索了半天也不會。。。。。

網上查找吧,各種介紹看着挺酷 但是 複製過來不好使啊。。。

一大哥說 兩個控件必須要進行關聯一下 。。。

又是一頓搜  搜完一頓摸索

還好,終於找到了,也發現了關鍵地方

上代碼

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/bt_a"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="1"
        android:textSize="12dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/bt_b" />

    <Button
        android:id="@+id/bt_b"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="2"
        android:textSize="12dp"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintLeft_toRightOf="@+id/bt_a"
        app:layout_constraintRight_toRightOf="parent" />


</android.support.constraint.ConstraintLayout>

 

重點的地方在兩處

1.

最左邊的要設置 app:layout_constraintLeft_toLeftOf"parent"

坐右邊的要設置 app:layout_constraintRight_toRightOf="parent"

2.

然後控件左邊的要設置我的右邊挨着他的左邊。。。 app:layout_constraintRight_toLeftOf="@+id/bt_b"
右邊的控件要設置我的左邊挨着他的右邊。。。。。app:layout_constraintLeft_toRightOf="@+id/bt_a"

然後就可以設置 

app:layout_constraintHorizontal_weight 屬性進行比例分割佈局了

ok可以愉快的佈局了!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章