android约束布局中 链,Android-ConstraintLayout(约束布局)-Chains链(链条布局,Nice)
到Chains這個部分了。之前的新項目做得登錄,注冊,重置密碼等暫時還沒用到這種。不過后面可能隨著新的設計可能會涉及到。所以趕緊過來看看先。新項目基本就打算全部用約束布局實現了。實際用了也會越來越熟悉的。
Chain鏈是一種特殊的約束讓多個 chain 鏈連接的 Views 能夠平分剩余空間位置像LinearLayout的權重,不過還擴展了很多功能。所以做權重什么的還是需要這種布局。*** 有點疑問?之前的Bias呢? 之前的Bias我們看下定義***:
The default when encountering such opposite constraints is to center the widget; but you can tweak the positioning to favor one side over another using the bias attributes:
Bias是用來做間距比例使用的,為了偏向另一邊更多一點。一般我們控件如果left、right都設置了都是居中的,如果你想靠左邊多一些,就可以用這個比例來設置。相對margin來講適配更好,畢竟采用的是比例。
image
如果要做三個控件均勻橫向排列,想想前幾篇的知識能不能做?相對布局,間距,中心布局,環形布局,尺寸相關約束,回過頭看了看官方網站回憶了下知識,發現好像不行。貌似這種只有權重能解決這個問題了,不然沒辦法均分?
Chains來了,別慌....
幾點要求滿足鏈式 (看下水平情況,垂直類似分析):
1. 控件之間已經形成約束關系,parent←A← →B← →C->parent
2.Chain heads(鏈條頭用來設置/覺得鏈條屬性),即是A作為鏈條頭
3.鏈條頭、鏈條尾分別是A和C,了解下對后面一些屬性有幫助
看看怎么設置鏈條布局樣式吧,水平垂直分別是layout_constraintHorizontal_chainStyle和layout_constraintVertical_chainStyle,分別三個屬性
image
1.水平的 Chain 鏈的默認模式就是spread模式,它將平分間隙讓多個 Views 布局到剩余空間。
app:layout_constraintHorizontal_chainStyle="spread":
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent">
android:id="@+id/sbBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sb"
app:layout_constraintEnd_toStartOf="@+id/sbBtn01"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/sbBtn01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sb01"
app:layout_constraintEnd_toStartOf="@+id/sbBtn02"
app:layout_constraintStart_toEndOf="@+id/sbBtn"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/sbBtn02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sb02"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/sbBtn01"
app:layout_constraintTop_toTopOf="parent" />
image
2.水平的 Chain 鏈的默認模式就是spread_inside模式,它將會把兩邊鏈條頭、鏈條尾分別是A和C 到外向父組件邊緣的距離去除,然后讓剩余的 Views 在剩余的空間內平分間隙布局
image
3.水平的 Chain 鏈的默認模式就是packed模式**,它將所有 Views 打包到一起不分配多余的間隙(當然不包括通過 margin 設置多個 Views 之間的間隙),然后將整個組件組在可用的剩余位置居中
image
以上三種樣式基本能滿足這種均分布局樣式。不過如果某個控件的寬度設置的不是wrap_content,而是一個具體的寬度dp。還比較大。這個時候布局就會有問題
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent">
android:id="@+id/sbBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sb"
app:layout_constraintEnd_toStartOf="@+id/sbBtn01"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/sbBtn01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sb01"
app:layout_constraintEnd_toStartOf="@+id/sbBtn02"
app:layout_constraintStart_toEndOf="@+id/sbBtn"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/sbBtn02"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="sb02"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/sbBtn01"
app:layout_constraintTop_toTopOf="parent" />
image
解決:我們需要把控件的寬度都設置為0dp, 這樣能保證控件依然均分,某個控件的內容如果超出寬度也不會影響均分效果。
image
當然你也可以一些控件自適應wrap_content,剩下的控件0dp,這樣就能保證自適應的控件內容顯示正常(不換行),但是設置為0dp的控件就會被強制各種壓迫:
image
So...注意結合相關知識才能應對更加復雜的情況
看看權重吧...這跟LinearLayout的weight權重設置很像,同樣也能做到和上面一樣的效果,也可以多分一點給第一個控件。不過也是需要設置寬度為0dp,否則大家都是wrap_content,就壓迫的啥都看不到了...
app:layout_constraintHorizontal_weight="2"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent">
android:id="@+id/sbBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="sb"
app:layout_constraintEnd_toStartOf="@+id/sbBtn01"
app:layout_constraintHorizontal_weight="2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/sbBtn01"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="sb02sb02sb02sb02sb02sb02sb02sb02"
app:layout_constraintEnd_toStartOf="@+id/sbBtn02"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@+id/sbBtn"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/sbBtn02"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="sb02sb02sb02sb02sb02"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@+id/sbBtn01"
app:layout_constraintTop_toTopOf="parent" />
image
最后關于margin設置:
Margins and chains (in 1.1)
When using margins on elements in a chain, the margins are additive.
For example, on a horizontal chain, if one element defines a right margin of 10dp and the next element defines a left margin of 5dp, the resulting margin between those two elements is 15dp.
An item plus its margins are considered together when calculating leftover space used by chains to position items. The leftover space does not contain the margins.
當用margin的時候是會有效的。在做均分時會先扣除間距,然后做均分。 差不多就是這樣一個解釋吧。哈哈哈。。。
基本上Chain布局就是差不多夠用了。復雜的布局可能需要結合之前的布局知識才能完成喲....約束布局確實方便很多.....
下篇看看Virtual Helper objects(Barrierhttps://zhuanlan.zhihu.com/p/37988125 就是一個灰常棒的例子),基本就短暫結束了。做項目妥妥了....
總結
以上是生活随笔為你收集整理的android约束布局中 链,Android-ConstraintLayout(约束布局)-Chains链(链条布局,Nice)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 体检,去医院可以只检查尿酸和甘油三酯,这
- 下一篇: kz,速戳...