1. 程式人生 > >ConstraintLayout — The Revolution

ConstraintLayout — The Revolution

ConstraintLayout is a big revolution in Android layout system. It provides several type of constraints to squeeze/positioning your views. You can see it as a much powerful RelativeLayout or if you familiar with iOS development — AutoLayout. Either way is trying to solve a math problem.

How to position a View

We knew that we only need x, y, width and hight to label a region in a 2-dimensional coordinates system like our View system. That is also the idea of the ancient AbsoluteLayout if you have heard of it long times ago. As you can imagine the hardcode positioning is not suitable for the variety of Android world.

Since AbsoluteLayout is deprecate due to lack of flexibility, LinearLayout is another solution to simplify the layout problem. LinearLayout only handle view in one dimension and fill them one by one. It’s an elegant solution especially in mobile world since many layout is a list-like structure.

LinearLayout

is good but if you want to deal with much complex layout you may need to nest it into different layer and layer which is not recommend. RelativeLayout on the other hand is more care about the relationship of each view, you can decide the view relation to other view to suit your need.