jetpack1
组合函数
package com.example.myapplication1
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import com.example.myapplication1.ui.theme.MyApplication1Theme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Text("______________________")
Greeting(name = "haha")
}
}
}
// 定义可组合函数
@Composable
fun Greeting(name: String){
Text(text = "name: $name")
}
// 添加预览
@Preview
@Composable
fun PreGreeting(){
Greeting(name = "xixi") // 预览中显示xixi,模拟器中显示haha,因为预览函数不会被调用
}
布局
package com.example.myapplication1
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NewsStory()
}
}
}
@Composable
fun NewsStory(){
// 垂直排列
Column(
modifier = Modifier.padding(16.dp) // 间距
) {
Image(
painter = painterResource(R.drawable.header),
contentDescription = null, // 给障碍人士的提示信息
modifier = Modifier
.height(180.dp)
.fillMaxWidth(),
contentScale = ContentScale.Crop // 适当裁剪
)
// 添加分割
Spacer(Modifier.height(16.dp))
Text("line 1")
Text("line 2")
Text("line 3")
}
}
// 不会被应用调用的预览函数
@Preview
@Composable
fun DefaultPreview(){
NewsStory()
}
Material Design
package com.example.myapplication1
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NewsStory()
}
}
}
@Composable
fun NewsStory(){
MaterialTheme() {
val typography = MaterialTheme.typography // 样式
// 垂直排列
Column(
modifier = Modifier.padding(16.dp) // 间距
) {
Image(
painter = painterResource(R.drawable.header),
contentDescription = null, // 给障碍人士的提示信息
modifier = Modifier
.height(180.dp)
.fillMaxWidth()
.clip(
shape = RoundedCornerShape(4.dp), // 图片圆角
),
contentScale = ContentScale.Crop // 适当裁剪
)
// 添加分割
Spacer(Modifier.height(16.dp))
Text(
"line 1" + "hhhhhhh hhhhhhh" + "xixixixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
style = typography.h6,
maxLines = 2,
overflow = TextOverflow.Ellipsis // 超过两行被截断
)
Text("line 2", style = typography.body2)
Text("line 3", style = typography.body2)
}
}
}
// 不会被应用调用的预览函数
@Preview
@Composable
fun DefaultPreview(){
NewsStory()
}
随机推荐
- MindSpore中使用model.train,在每一步训练结束后自动进行调用自定义函数 —— from mindspore.train.callback import Callback
在MindSpore中使用model.train训练网络时我们难以处理间断性的任务,为此我们可以考虑使用MindSpore中的Callback机制.Callback 函数可以在 model.train ...
- 再探 游戏 《 2048 》 —— AI方法—— 缘起、缘灭(8) —— 2021年9月SOTA的TDL算法——《Optimistic Temporal Difference Learning for 2048》——完结篇
<2048>游戏在线试玩地址: https://play2048.co/ 如何解决<2048>游戏源于外网的一个讨论帖子,而这个帖子则是讨论如何解决该游戏的最早开始,可谓是&q ...
- Camera | 8.让rk3568支持前后置摄像头
一.目标 本文主要目标是,支持前置摄像头0v5648.后置摄像头ov13850,以及移植过程遇到的一些小问题的解决. 1. 摄像头连接图 参考上图,摄像头详细信息如下: 2个摄像头均连接在I2C通道4 ...
- WIN32下的模拟时钟
#include <Windows.h> #include <math.h> #include <tchar.h> #include "resource. ...
- C# WebSocket Fleck 源码解读
最近在维护公司旧项目,偶然发现使用Fleck实现的WebSocket主动推送功能,(由于前端页面关闭时WebSocket Server中执行了多次OnClone事件回调并且打印了大量的关闭日志,),后 ...
- drawable xx should not reference itself
背景: 在Android中新增一个xx.xml,在layer-list 的item中设定引入的drawable后,报这个提示(xx不能引用自身) 原因: 这个错误其实很离谱,但是还是有必要记一下,万一 ...
- LLM大模型基础知识学习总结
大家好,我是Edison. 在这个已经被AI大模型包围的时代,不了解一点大模型的基础知识和相关概念,可能出去聊天都接不上话.刚好近期我也一直在用GPT和GitHub Copilot,也刚好对这些基础知 ...
- RGB、HSV和HSL颜色空间
这个文章写的很清楚了 https://zhuanlan.zhihu.com/p/67930839
- vue3 如何获取 dom
1. 通过 ref 1. 在 html 标签上指定 ref 属性 2. 在 setup 中定义并返回.注意:标签上的 ref 属性名需要跟 setup 中的对应 <h1 ref= ...
- 一文快速上手-Vue CLI脚手架
目录 安装Vue CLI Vue CLI新建项目 vue.js 3 项目目录结构 项目的运行和打包 vue.config.js文件解析 安装Vue CLI (1) 全局安装Vue CLI 方式一(推荐 ...