当前位置: 首页 > news >正文

中小企业网站建设公司做网站点击挣钱不

中小企业网站建设公司,做网站点击挣钱不,wordpress hook api,WordPress主题getJetpackCompose从入门到实战学习笔记8—ConstraintLayout的简单使用 1.简介: Compose 中的 ConstraintLayout ConstraintLayout 是一种布局,让您可以相对于屏幕上的其他可组合项来放置可组合项。它是一种实用的替代方案,可代替使用多个已嵌…

JetpackCompose从入门到实战学习笔记8—ConstraintLayout的简单使用

1.简介:

Compose 中的 ConstraintLayout

ConstraintLayout 是一种布局,让您可以相对于屏幕上的其他可组合项来放置可组合项。它是一种实用的替代方案,可代替使用多个已嵌套的 RowColumnBox 和其他自定义布局元素这种做法。在实现对齐要求比较复杂的较大布局时,ConstraintLayout 很有用。

在以下情况下,考虑使用 ConstraintLayout

  • 为了避免在屏幕上定位元素时嵌套多个 ColumnRow,以便提高代码的可读性。
  • 相对于其它可组合项来定位可组合项,或根据引导线、屏障线或链来定位可组合项。

在 View 系统中,建议使用 ConstraintLayout 来创建复杂的大型布局,因为扁平视图层次结构比嵌套视图的效果更好。不过,这在 Compose 中不是什么问题,因为 Compose 能够高效地处理较深的布局层次结构。

2.在build.gradle中添加依赖:

    implementation "androidx.constraintlayout:constraintlayout-compose:1.0.1"

3.可组合项简单使用:

@Composable
fun ConstraintLayoutContent() {ConstraintLayout {// Create references for the composables to constrainval (button, text) = createRefs()Button(onClick = { /* Do something */ },// Assign reference "button" to the Button composable// and constrain it to the top of the ConstraintLayoutmodifier = Modifier.constrainAs(button) {top.linkTo(parent.top, margin = 16.dp)}) {Text("Button")}// Assign reference "text" to the Text composable// and constrain it to the bottom of the Button composableText("Android", Modifier.constrainAs(text) {top.linkTo(button.bottom, margin = 16.dp)})}
}

4.效果如下:

在这里插入图片描述

5.Barrier分界线:

    @Preview@Composablefun InputFieldLayoutDemo() {ConstraintLayout(modifier = Modifier.width(400.dp).height(100.dp).padding(10.dp)) {val (usernameTextRef, passwordTextRef, usernameInputRef, passWordInputRef, dividerRef) = remember { createRefs() }var barrier = createEndBarrier(usernameTextRef, passwordTextRef)Text(text = "用户名",fontSize = 14.sp,textAlign = TextAlign.Left,modifier = Modifier.constrainAs(usernameTextRef) {top.linkTo(parent.top)start.linkTo(parent.start)})Divider(Modifier.fillMaxWidth().constrainAs(dividerRef) {top.linkTo(usernameTextRef.bottom)bottom.linkTo(passwordTextRef.top)})Text(text = "密码",fontSize = 14.sp,modifier = Modifier.constrainAs(passwordTextRef) {top.linkTo(usernameTextRef.bottom, 19.dp)start.linkTo(parent.start)})OutlinedTextField(value = "",onValueChange = {},modifier = Modifier.constrainAs(usernameInputRef) {start.linkTo(barrier, 10.dp)top.linkTo(usernameTextRef.top)bottom.linkTo(usernameTextRef.bottom)height = Dimension.fillToConstraints})OutlinedTextField(value = "",onValueChange = {},modifier = Modifier.constrainAs(passWordInputRef) {start.linkTo(barrier, 10.dp)top.linkTo(passwordTextRef.top)bottom.linkTo(passwordTextRef.bottom)height = Dimension.fillToConstraints})}}

6.效果如下:

在这里插入图片描述

7.GuideLine引导线:

    @Preview@Composablefun GuidelineSample(){ConstraintLayout(modifier = Modifier.fillMaxSize().padding(10.dp).background(Color.White)) {val topGuideline = createGuidelineFromTop(0.2f)var (userPortraitBackgroundRef, userPortraitImgRef, welcomeRef, quotesRef) = remember { createRefs() }Box(modifier = Modifier.background(Color.Green).constrainAs(userPortraitBackgroundRef) {top.linkTo(parent.top)bottom.linkTo(topGuideline)height = Dimension.fillToConstraintswidth = Dimension.matchParent})Image(painter = painterResource(id = R.mipmap.avatar),contentDescription = stringResource(R.string.description),contentScale = ContentScale.Crop,modifier = Modifier.constrainAs(userPortraitImgRef){top.linkTo(topGuideline)bottom.linkTo(topGuideline)start.linkTo(parent.start)end.linkTo(parent.end)}.size(100.dp).clip(CircleShape).border(width = 2.dp, color = Color.Red, shape = CircleShape))Text(text = "Compose 技术爱好者Compose 技术爱好者Compose 技术爱好者Compose 技术爱好者Compose 技术爱好者Compose ",fontSize = 24.sp,maxLines = 1,textAlign = TextAlign.Center,overflow = TextOverflow.Ellipsis,//1行显示不下时是否显示。。。modifier = Modifier.constrainAs(welcomeRef) {top.linkTo(userPortraitImgRef.bottom, 30.dp)start.linkTo(parent.start)end.linkTo(parent.end)width = Dimension.preferredWrapContent})}}//这里有个小技巧:若要想TextView一行铺满但没有完全显示且显示。。。时设置 overflow = TextOverflow.Ellipsis属性

8.效果如下:

在这里插入图片描述

9.Chain链接约束:

 /***  Chain链接约束,Compose提供了ChainStyle*  1.Spread:链条中美国元素均分整个parent空间。*  2.SpreadInside:链条中首尾元素紧贴边界,剩下每个元素平分整个parent空间。*  3.Packed:链条在所有元素聚焦到中间。*/@Preview@Composablefun ChainDemo() {ConstraintLayout(modifier = Modifier.fillMaxSize().background(Color.Gray)) {val (quotesFirstLineRef, quotesSecondLineRef, quotesThirdLineRef, quotesForthLineRef) = remember { createRefs() }createVerticalChain(quotesFirstLineRef, quotesSecondLineRef, quotesThirdLineRef, quotesForthLineRef,chainStyle = ChainStyle.SpreadInside)Text(text = "寄蜉蝣于天地,",color = Color.White,fontSize = 30.sp,fontWeight = FontWeight.Bold,modifier = Modifier.constrainAs(quotesFirstLineRef) {start.linkTo(parent.start)end.linkTo(parent.end)})Text(text = "渺沧海之一粟。",color = Color.White,fontSize = 30.sp,fontWeight = FontWeight.Bold,modifier = Modifier.constrainAs(quotesSecondLineRef) {start.linkTo(parent.start)end.linkTo(parent.end)})Text(text = "哀吾生之须臾,",color = Color.White,fontSize = 30.sp,fontWeight = FontWeight.Bold,modifier = Modifier.constrainAs(quotesThirdLineRef) {start.linkTo(parent.start)end.linkTo(parent.end)})Text(text = "羡长江之无穷。",color = Color.White,fontSize = 30.sp,fontWeight = FontWeight.Bold,modifier = Modifier.constrainAs(quotesForthLineRef) {start.linkTo(parent.start)end.linkTo(parent.end)})}}

10.Spread模式效果如下:

  • Spread模式:链条中美国元素均分整个parent空间。

在这里插入图片描述

11.SpreadInside模式效果如下:

  • SpreadInside:链条中首尾元素紧贴边界,剩下每个元素平分整个parent空间。

在这里插入图片描述

12.Packed模式效果如下:

  • Packed:链条在所有元素聚焦到中间。

在这里插入图片描述

13.完整代码如下:

import android.os.Bundle
import android.widget.ScrollView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ChainStyle
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension/*** @auth: njb* @date: 2023/1/17 16:36* @desc:*/
class ConstraintLayoutActivity:ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContent {// ConstraintLayoutContent()// ConstraintLayoutSample()//InputFieldLayoutDemo()//GuidelineSample()ChainDemo()}}@Preview@Composablefun ConstraintLayoutContent() {ConstraintLayout {// Create references for the composables to constrainval (button, text) = createRefs()Button(onClick = { /* Do something */ },// Assign reference "button" to the Button composable// and constrain it to the top of the ConstraintLayoutmodifier = Modifier.constrainAs(button) {top.linkTo(parent.top, margin = 16.dp)}) {Text("Button")}// Assign reference "text" to the Text composable// and constrain it to the bottom of the Button composableText("Android", Modifier.constrainAs(text) {top.linkTo(button.bottom, margin = 16.dp)})}}@Preview@Composablefun  ConstraintLayoutSample(){ConstraintLayout(modifier = Modifier.width(400.dp).height(300.dp).padding(10.dp)) {// Create references for the composables to constrainval (button, text) = createRefs()Button(onClick = { },// Assign reference "button" to the Button composable// and constrain it to the top of the ConstraintLayoutmodifier = Modifier.constrainAs(button) {top.linkTo(parent.top, margin = 6.dp)}) {Text("Button")}// Assign reference "text" to the Text composable// and constrain it to the bottom of the Button composableText("Android", Modifier.constrainAs(text) {top.linkTo(button.bottom, margin = 6.dp)})val (portraitImageRef, usernameTextRef, desTextRef) = remember { createRefs() }Image(painter = painterResource(id = R.mipmap.avatar),contentDescription = stringResource(R.string.description),contentScale = ContentScale.Crop,modifier = Modifier.constrainAs(portraitImageRef){top.linkTo(parent.top)bottom.linkTo(parent.bottom)}.size(100.dp))Text(text = "Compose 技术爱好者Compose 技术爱好者Compose 技术爱好者Compose 技术爱好者Compose 技术爱好者Compose ",fontSize = 14.sp,maxLines = 1,textAlign = TextAlign.Center,overflow = TextOverflow.Ellipsis,//1行显示不下时是否显示。。。modifier = Modifier.constrainAs(usernameTextRef) {top.linkTo(portraitImageRef.top)start.linkTo(portraitImageRef.end, 10.dp)end.linkTo(parent.end, 10.dp)width = Dimension.preferredWrapContent})Text(text = "我的个人描述...",fontSize = 14.sp,color = Color.Red,fontWeight = FontWeight.Light,modifier = Modifier.constrainAs(desTextRef) {top.linkTo(usernameTextRef.bottom, 5.dp)start.linkTo(portraitImageRef.end, 10.dp)})}}@Preview@Composablefun GuidelineSample(){ConstraintLayout(modifier = Modifier.fillMaxSize().padding(10.dp).background(Color.White)) {val topGuideline = createGuidelineFromTop(0.2f)var (userPortraitBackgroundRef, userPortraitImgRef, welcomeRef, quotesRef) = remember { createRefs() }Box(modifier = Modifier.background(Color.Green).constrainAs(userPortraitBackgroundRef) {top.linkTo(parent.top)bottom.linkTo(topGuideline)height = Dimension.fillToConstraintswidth = Dimension.matchParent})Image(painter = painterResource(id = R.mipmap.avatar),contentDescription = stringResource(R.string.description),contentScale = ContentScale.Crop,modifier = Modifier.constrainAs(userPortraitImgRef){top.linkTo(topGuideline)bottom.linkTo(topGuideline)start.linkTo(parent.start)end.linkTo(parent.end)}.size(100.dp).clip(CircleShape).border(width = 2.dp, color = Color.Red, shape = CircleShape))Text(text = "Compose 技术爱好者Compose 技术爱好者Compose 技术爱好者Compose 技术爱好者Compose 技术爱好者Compose ",fontSize = 24.sp,maxLines = 1,textAlign = TextAlign.Center,overflow = TextOverflow.Ellipsis,//1行显示不下时是否显示。。。modifier = Modifier.constrainAs(welcomeRef) {top.linkTo(userPortraitImgRef.bottom, 30.dp)start.linkTo(parent.start)end.linkTo(parent.end)width = Dimension.preferredWrapContent})}}@Preview@Composablefun InputFieldLayoutDemo() {ConstraintLayout(modifier = Modifier.width(400.dp).height(100.dp).padding(10.dp)) {val (usernameTextRef, passwordTextRef, usernameInputRef, passWordInputRef, dividerRef) = remember { createRefs() }var barrier = createEndBarrier(usernameTextRef, passwordTextRef)Text(text = "用户名",fontSize = 14.sp,textAlign = TextAlign.Left,modifier = Modifier.constrainAs(usernameTextRef) {top.linkTo(parent.top)start.linkTo(parent.start)})Divider(Modifier.fillMaxWidth().constrainAs(dividerRef) {top.linkTo(usernameTextRef.bottom)bottom.linkTo(passwordTextRef.top)})Text(text = "密码",fontSize = 14.sp,modifier = Modifier.constrainAs(passwordTextRef) {top.linkTo(usernameTextRef.bottom, 19.dp)start.linkTo(parent.start)})OutlinedTextField(value = "",onValueChange = {},modifier = Modifier.constrainAs(usernameInputRef) {start.linkTo(barrier, 10.dp)top.linkTo(usernameTextRef.top)bottom.linkTo(usernameTextRef.bottom)height = Dimension.fillToConstraints})OutlinedTextField(value = "",onValueChange = {},modifier = Modifier.constrainAs(passWordInputRef) {start.linkTo(barrier, 10.dp)top.linkTo(passwordTextRef.top)bottom.linkTo(passwordTextRef.bottom)height = Dimension.fillToConstraints})}}/***  Chain链接约束,Compose提供了ChainStyle*  1.Spread:链条中美国元素均分整个parent空间*  2.SpreadInside:*  3.Packed:*/@Preview@Composablefun ChainDemo() {ConstraintLayout(modifier = Modifier.fillMaxSize().background(Color.Gray)) {val (quotesFirstLineRef, quotesSecondLineRef, quotesThirdLineRef, quotesForthLineRef) = remember { createRefs() }createVerticalChain(quotesFirstLineRef, quotesSecondLineRef, quotesThirdLineRef, quotesForthLineRef,chainStyle = ChainStyle.Packed)Text(text = "寄蜉蝣于天地,",color = Color.White,fontSize = 30.sp,fontWeight = FontWeight.Bold,modifier = Modifier.constrainAs(quotesFirstLineRef) {start.linkTo(parent.start)end.linkTo(parent.end)})Text(text = "渺沧海之一粟。",color = Color.White,fontSize = 30.sp,fontWeight = FontWeight.Bold,modifier = Modifier.constrainAs(quotesSecondLineRef) {start.linkTo(parent.start)end.linkTo(parent.end)})Text(text = "哀吾生之须臾,",color = Color.White,fontSize = 30.sp,fontWeight = FontWeight.Bold,modifier = Modifier.constrainAs(quotesThirdLineRef) {start.linkTo(parent.start)end.linkTo(parent.end)})Text(text = "羡长江之无穷。",color = Color.White,fontSize = 30.sp,fontWeight = FontWeight.Bold,modifier = Modifier.constrainAs(quotesForthLineRef) {start.linkTo(parent.start)end.linkTo(parent.end)})}}
}

14.完整效果预览如下:

在这里插入图片描述

http://www.yayakq.cn/news/253334/

相关文章:

  • 沙坪坝网站建设哪家好wordpress 数据库导出
  • 网站设计建设 网络营销推广如果做好网站社区的建设
  • 高埗仿做网站大连网站制作赞ls15227
  • 浙江省邮电工程建设有限公司 网站如何建立自己的平台
  • 建设网站的风险正规的培训学校
  • 网站建设交印花税吗微信营销方式有哪些
  • 图片展示类网站百度收录
  • 湖北黄石网站建设python 做网站 案例
  • 宁夏网站建设优化王烨萌 俄罗斯
  • 建材网站方案班级网站 模板
  • 做外贸网站注册什么邮箱鞋材 技术支持 东莞网站建设
  • 如何用dede做带下单的网站织梦汽车网站模板免费下载
  • 网站设计师专业做一个网站要花多少钱
  • 网站开发接口文档模板文字游戏做的最好的网站
  • 济南免费网站建站模板贵阳网站seo
  • 上海网站分站建设新网网站备案流程
  • 烟台企业做网站网络舆情监测中心具体做什么
  • 网站怎样做移动端适配网页设计总结5000字
  • 表白网页制作免费网站制作苏州吴中区做网站的
  • 做直播网站需要证书吗做律师咨询网站
  • 华为做网站吗中国食品加工网
  • 烟台网站公司医疗设备响应式网站
  • 手游网站建设的宗旨网站策划书包括哪几个步骤
  • 苏州网站建设搭建注册公司流程和费用公司注册
  • 济南建网站公司价格小红书推广营销
  • 网站开发的学习路线达州网站建设
  • 湖州建设局投标网站推广普通话绘画
  • 庐江县建设局网站合肥霍山路网站建设
  • 公司网站备案有什么用wordpress更多
  • 旅游网站制作文献世界各国黄页