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()
}
随机推荐
- Python报错:WARNING conda.models.version:get_matcher(542): Using .* with relational operator is superfluous and deprecated and will be removed in a future version of conda.
参考: https://blog.csdn.net/weixin_45685859/article/details/132916216 报错: [23:59:14](pytorch) devil@OM ...
- Ubuntu22.04下vscode安装python代码格式化(Format Document)black模块及设置
相关: 如何在vscode中支持python的annotation(注解,type checking)--通过设置pylance参数实现python注解的type checking ubuntu22. ...
- [CEOI2010 day2] tower 题解
前言 题目链接:洛谷. 题意简述 你要对一个数组排序,满足 \(a_{i + 1} \leq a_i + D\),其中 \(D\) 是给定的常数.求方案数对 \(10^9+9\) 取模的结果. 题目分 ...
- 代码随想录Day10
232.用栈实现队列 请你仅使用两个栈实现先入先出队列.队列应当支持一般队列支持的所有操作(push.pop.peek.empty): 实现 MyQueue 类: void push(int x) 将 ...
- rest_framework与django配合使用
rest_framework与django配合使用 rest_framework与django配合使用 一.构建表单,在这里我们先构建五个表单,分别是 author book publish us ...
- SMU Autumn 2023 Round 5
SMU Autumn 2023 Round 5 A. Everyone Loves to Sleep 把时间都转成分钟,然后存起来,二分找到离他睡觉点最近的一个时间段,减去他的睡觉点,如果最近的在第二 ...
- Prometheus部署以及问题解决
Prometheus作用: Prometheus监控(Prometheus Monitoring)是一种开源的系统监控和警报工具.它最初由SoundCloud开发并于2012年发布,并在2016年加入 ...
- CSEC:香港城市大学提出SOTA曝光矫正算法 | CVPR 2024
在光照条件不佳下捕获的图像可能同时包含过曝和欠曝.目前的方法主要集中在调整图像亮度上,这可能会加剧欠曝区域的色调失真,并且无法恢复过曝区域的准确颜色.论文提出通过学习估计和校正这种色调偏移,来增强既有 ...
- LaTeX 编译警告:Script 'CJK' not explicitly supported within font 'FandolSong-Regular'. Check the typeset output, and if it is okay then ignore this warning. Otherwise a different font should be chosen.
在编译一篇中文文档时遇到如下警告: Package fontspec Warning: Script 'CJK' not explicitly supported within font 'Fando ...
- [golang]查询ssl证书剩余有效天数并邮件提醒
前言 自从云厂商的免费ssl证书改成3个月,而且证书数量还是20个之后,自己网站的ssl证书就换成了其它免费方案.但是免费方案不会提醒证书过期,所以写个工具每天定时查询证书剩余有效天数,如果证书即将过 ...