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

android开发中的 AndroidX 版本的查看 及 constraintLayout的简单用法

1、查看库的版本

平常我们经常会用到一些库,但是不知道是什么版本,也不知道最新的是什么版本,当然最好的就是到官网去查看,或者三方的maven库。

2、官方地址

AndroidX 版本  |  Jetpack  |  Android Developers

3、比如我们来看一下constraintlayout

4、引入到项目libs

androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout-compose", version.ref = "constraintLayout" }constraintLayout = "1.1.1"

5、引入项目

implementation(libs.androidx.constraintlayout)

6、写个demo

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dpimport android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.TextFieldValue
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import kotlinx.coroutines.launch@Composable
fun SalesScreen() {val scope = rememberCoroutineScope()val context = LocalContext.currentvar selectedCategoryId by remember { mutableStateOf(0L) }var searchText by remember { mutableStateOf(TextFieldValue("")) }// 模拟分类数据val categories = listOf(Category(1, "饮料"),Category(2, "零食"),Category(3, "日用品"))// 模拟商品数据val goodsList = remember(selectedCategoryId, searchText) {getGoodsByCategoryAndSearch(selectedCategoryId, searchText.text)}ConstraintLayout(modifier = Modifier.fillMaxSize()) {val (categoryList, goodsHeader, goodsListSection) = createRefs()// 左侧分类列表LazyColumn(modifier = Modifier.constrainAs(categoryList) {start.linkTo(parent.start)top.linkTo(parent.top)bottom.linkTo(parent.bottom)width = Dimension.percent(0.3f)}.padding(8.dp)) {items(categories) { category ->CategoryItem(category = category,isSelected = category.id == selectedCategoryId,onClick = {selectedCategoryId = category.id})}}// 右侧商品搜索栏TextField(value = searchText,onValueChange = { searchText = it },label = { Text("搜索商品") },modifier = Modifier.constrainAs(goodsHeader) {start.linkTo(categoryList.end)end.linkTo(parent.end)top.linkTo(parent.top)width = Dimension.fillToConstraints}.padding(8.dp))// 右侧商品列表LazyColumn(modifier = Modifier.constrainAs(goodsListSection) {start.linkTo(categoryList.end)top.linkTo(goodsHeader.bottom)end.linkTo(parent.end)bottom.linkTo(parent.bottom)width = Dimension.percent(0.7f)}.padding(8.dp)) {items(goodsList) { good ->GoodItem(good = good)}}}
}// 分类数据类
data class Category(val id: Long, val name: String)// 商品数据类
data class Good(val id: Long, val name: String, val price: Double)// 分类项组件
@Composable
fun CategoryItem(category: Category, isSelected: Boolean, onClick: () -> Unit) {Card(modifier = Modifier.fillMaxWidth().padding(4.dp).clickable { onClick() },colors = if (isSelected) CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primaryContainer)else CardDefaults.cardColors()) {Text(text = category.name,modifier = Modifier.padding(12.dp))}
}// 商品项组件
@Composable
fun GoodItem(good: Good) {Card(modifier = Modifier.fillMaxWidth().padding(4.dp)) {Row(modifier = Modifier.padding(12.dp)) {Text(text = good.name, style = MaterialTheme.typography.titleMedium)Spacer(modifier = Modifier.weight(1f))Text(text = "¥${good.price}", style = MaterialTheme.typography.titleSmall)}}
}// 模拟根据分类和搜索条件获取商品列表
fun getGoodsByCategoryAndSearch(categoryId: Long, query: String): List<Good> {// 假设原始数据val allGoods = mapOf(1L to listOf(Good(1, "可乐", 3.5),Good(2, "雪碧", 3.5),Good(3, "矿泉水", 2.0)),2L to listOf(Good(4, "薯片", 5.0),Good(5, "巧克力", 8.0),Good(6, "饼干", 4.5)),3L to listOf(Good(7, "牙膏", 9.9),Good(8, "洗发水", 19.9),Good(9, "纸巾", 5.0)))return allGoods[categoryId]?.filter {it.name.contains(query, ignoreCase = true)} ?: emptyList()
}

7、demo运行截图

demo运行的效果,还是不错的,点击左侧,能显示右侧的列表,相当可以。

http://www.dtcms.com/a/267309.html

相关文章:

  • 【性能优化】程序性能优化:疏通胜于堵塞
  • 【Elasticsearch】检索高亮
  • 成为git砖家(12): 看懂git合并分支时冲突提示符
  • HTML初学者第三天
  • hono框架绑定cloudflare的d1数据库操作步骤
  • Redis基础的介绍与使用(一)(Redis简介以及Redis下载和安装)
  • Git 版本控制完全指南:从入门到精通
  • 【Halcon】WPF 自定义Halcon显示控件完整流程与 `OnApplyTemplate` 未触发的根本原因解析!
  • Web3 Study Log 003
  • 蓝牙墨水屏上位机学习(3)
  • Java 与 Vue 全栈开发:“一课一得“ 学习笔记系统实战
  • OneCode图表配置速查手册
  • CMake是什么
  • NV183NV185美光固态闪存NV196NV201
  • 供应链管理-采购管理:国际贸易及支付领域中常见的支持方式
  • FLUX.1-Kontext 高效训练 LoRA:释放大语言模型定制化潜能的完整指南
  • 软件版本FCCU(故障采集与控制单元)设计
  • 如何选择不会降低网站速度的WordPress主题
  • 动手实践OpenHands系列学习笔记11:现代开发流程
  • C#指针:解锁内存操作的底层密码
  • DVWA靶场通关笔记-验证码绕过reCAPTCHA(Medium级别)
  • 网安系列【6】之[特殊字符] SQL注入揭秘:从入门到防御实战指南
  • cloudflare配合github搭建免费开源影视LibreTV一个独享视频网站 详细教程
  • React Native 亲切的组件们(函数式组件/class组件)和陌生的样式
  • 百度开源文心一言4.5:论文解读和使用入门
  • 闲庭信步使用SV搭建图像测试平台:第三十二课——系列结篇语
  • 【学习笔记】MySQL技术内幕InnoDB存储引擎——第5章 索引与算法
  • MySQL(118)如何使用SSL进行加密连接?
  • 前端进阶之路-从传统前端到VUE-JS(第三期-VUE-JS配套UI组件的选择)(Element Plus的构建)
  • vscode remote-ssh 拓展免密访问 linux虚拟机