当前位置:网站首页>Jetpack compose layout (IV) - constraintlayout
Jetpack compose layout (IV) - constraintlayout
2022-06-25 09:52:00 【seevc】
The previous articles introduced layout related : Layout Basics 、Material Components and layout 、 Custom layout .
In this article, we will continue to introduce the last knowledge point of layout :Compose Use in ConstraintLayout.
ConstraintLayout It helps to place composable items on the screen according to their relative positions , Using multiple nesting Row、Column、Box And custom layout elements can be replaced by this layout . When implementing large layouts with complex alignment requirements ,ConstraintLayout It is useful to . similar Android View In the system ConstrantLayout Layout .
Use Compose Medium ConstraintLayout when , Need to be in build.gradle The following libraries are introduced into :
implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0"
For specific version, please refer to :ConstraintLayout Version page
* notes : stay Android View The system uses ConstraintLayout To reduce layout nesting levels , Lifting performance , But in Compose UI There is no such problem in the system , It can efficiently handle deep layout hierarchies . So in Compose Whether to use ConstraintLayout It all depends on the developer's usage habits 、 Code legibility and maintainability .
Let's take a look at the usage :
Mode one : Use modifiers in composable items to set in-line
Use steps :
Step one 、 Use createRefs() or createRef() Create a reference associated with the component
Step two 、 By giving constraintAs Modifier sets the reference associated with it
Step three 、 call linkTo() Set constraints
parent Is an existing reference , Can be used to specify a pair of ConstraintLayout Constraints on composable items themselves .
example :
/**
* Mode one : Directly in ConstraintLayout Set constraint information internally in
*/
@Composable
fun StudyConstraintLayout(){
......
// Constraint Layout
ConstraintLayout(
modifier = Modifier.fillMaxSize()
) {
// Step one 、 Use createRefs() or createRef() Create a reference associated with the component
val (textTitle,tfUser,tfPassword,btnLogin,snack) = createRefs()
Text(
text = " Login screen ",
modifier = Modifier
// Step two 、 By giving constraintAs Modifier sets the reference associated with it
.constrainAs(textTitle) {
// Step three 、 call linkTo() Set constraints
start.linkTo(parent.start)
end.linkTo(parent.end)
// Set the top constraint and margin
top.linkTo(parent.top,20.dp)
}
)
......
}
}
Given the amount of sample code , Only part of the code is posted here , Detailed code (StudyConstraintLayout)
Running effect :
Mode two : Constraints and layout separation methods
In some cases we need to separate the constraints from the applied layout , Easy to use . Such as : Change constraints according to screen configuration , Or add animation effects between two constraint sets .
For such application scenarios, you can use ConstraintSet+Modifier.layoutId Combined way to achieve . The specific steps are as follows :
- Generate a
ConstraintSetobject , And pass it as a parameter toConstraintLayout; - Use
layoutIdThe modifier will be inConstraintSetReferences created in are assigned to composable items ;
Examples are as follows :
/**
* Mode two 、 By using ConstraintSet Set constraints
*/
@Composable
fun StudyConstraintSet(){
val constraintSet = decoupledConstraints(0.dp)
ConstraintLayout(constraintSet) {
Button(
onClick = { /* Do something */ },
// Notification settings layoutId Implement control constraints
modifier = Modifier.layoutId("button")
) {
Text("Button")
}
Text("Text", Modifier.layoutId("text"))
}
}
/**
* Create a ConstraintSet example
*/
private fun decoupledConstraints(margin:Dp):ConstraintSet{
return ConstraintSet {
val button = createRefFor("button")
val text = createRefFor("text")
constrain(button){
top.linkTo(parent.top)
}
constrain(text){
top.linkTo(button.bottom,10.dp)
}
}
}
// preview
@Preview(name = " Mode two ConstraintSet")
@Composable
fun PreviewStudyConstraintLayout2(){
StudyConstraintSet()
}
design sketch :
Come here Compose Of ConstrantLayout The content has been introduced .
Welcome to leave a message , Learning together , Common progress !
边栏推荐
- Swift recursively queries the array for the number closest to the specified value
- Is it safe to open an account online? Who can I ask?
- 2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
- How do dating applets make millions a year? What is the profit model?
- SparseArray details
- Is it safe to open an account with Great Wall Securities by mobile phone?
- MySQL创建给出语句
- js工具函数,自己封装一个节流函数
- [zufe school competition] difficulty classification and competition suggestions of common competitions in the school (taking Zhejiang University of Finance and economics as an example)
- [MySQL learning notes 21] storage engine
猜你喜欢

x86电脑上下载debian的arm64的包
![[zufe school competition] difficulty classification and competition suggestions of common competitions in the school (taking Zhejiang University of Finance and economics as an example)](/img/ee/2b8aebc1c63902d4d85ff71fd45070.jpg)
[zufe school competition] difficulty classification and competition suggestions of common competitions in the school (taking Zhejiang University of Finance and economics as an example)

Is it harder to find a job in 2020? Do a good job in these four aspects and find a good job with high salary

【mysql学习笔记22】索引
![[2020 cloud development + source code] 30 minutes to create and launch wechat applet practical project | zero cost | cloud database | cloud function](/img/89/39851fee714be872a599ad4ddd4852.jpg)
[2020 cloud development + source code] 30 minutes to create and launch wechat applet practical project | zero cost | cloud database | cloud function

How to download the school logo, school name and corporate logo on a transparent background without matting

Data-driven anomaly detection and early warning of 21 May Day C

Wearable devices may reveal personal privacy

Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021
![[wechat applet full stack development course] course directory (mpvue+koa2+mysql)](/img/1c/ab39cf0a69d90a85665a099f5dbd26.jpg)
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)
随机推荐
[buuctf.reverse] 121-125
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
Flutter dialog: cupertinoalertdialog
[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application
[IOU] intersection over union
Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days
Creo makes a mobius belt in the simplest way
[shared farm] smart agriculture applet, customized development and secondary development of Kaiyuan source code, which is more appropriate?
An auxiliary MVP architecture project quick development library -mvpfastdagger
测试开发工程师
纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口
2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course
Best producer consumer code
瑞吉外卖项目(二)
CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
【mysql学习笔记22】索引
[learn C from me and master the key to programming] insertion sort of eight sorts
Set the location permission in the shutter to "always allow"
Swift recursively queries the array for the number closest to the specified value
Flutter replaces the default icon of Gaud positioning