当前位置:网站首页>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
ConstraintSet
object , And pass it as a parameter toConstraintLayout
; - Use
layoutId
The modifier will be inConstraintSet
References 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 !
边栏推荐
- 51 SCM time stamp correlation function
- [MySQL learning notes 22] index
- Simple waterfall effect
- Is it safe for Huatai Securities to open an account on it? Is it reliable?
- 瑞吉外卖项目(二)
- 22 mathematical modeling contest 22 contest C
- Chitubox micromake l3+ slicing software configuration correspondence
- Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!
- Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
- Is it safe to open a stock account on the compass?
猜你喜欢
Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
[buuctf.reverse] 117-120
CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
Etcd tutorial - Chapter 4 etcd cluster security configuration
Applet cloud development joint table data query and application in cloud function
Download the arm64 package of Debian on X86 computer
汇付国际为跨境电商赋能:做合规的跨境支付平台!
Flutter dialog: cupertinoalertdialog
随机推荐
Prediction of pumpkin price based on BP neural network
Lvs-dr mode single network segment case
[buuctf.reverse] 121-125
Jetpack compose layout (I) - basic knowledge of layout
How to "transform" small and micro businesses (II)?
Is it safe to open a stock account on the compass?
Vscode attempted to write the procedure to a pipeline that does not exist
Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days
203 postgraduate entrance examination Japanese self-study postgraduate entrance examination experience post; Can I learn Japanese by myself?
Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
How do dating applets make millions a year? What is the profit model?
MySQL创建给出语句
Force buckle -104 Maximum depth of binary tree
Fluent creates, reads and writes JSON files
Why should the terminal retail industry choose the member management system
Analysis on the thinking of 2022 meisai C question
Is it safe to open a stock account through the account opening QR code of the account manager?
vscode试图过程写入管道不存在
Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later
Download the arm64 package of Debian on X86 computer