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()
}
随机推荐
- 【转载】 numpy数据类型dtype转换
原文地址: https://www.cnblogs.com/hhh5460/p/5129032.html =============================================== ...
- 对国产AI计算框架要有一定的包容力——记“mindspore”使用过程中的“不良反应”
看mindspore的官方文档,居然有502错误,恶心到了: 打开Eager模式的链接,报错:
- 强化学习中Q-learning,DQN等off-policy算法不需要重要性采样的原因
在整理自己的学习笔记的时候突然看到了这个问题,这个问题是我多年前刚接触强化学习时候想到的问题,之后由于忙其他的事情就没有把这个问题终结,这里也就正好把这个问题重新的规整一下. 其实,这个DQN算法作为 ...
- [NOI2007] 项链工厂 题解
前言 题目链接:洛谷:Hydro & bzoj. 题意简述 yzh 喜欢写 DS 题!你要维护一个环: 顺时针移动 \(k\) 位: 翻转 \(2 \sim n\): 交换 \(i\) 与 \ ...
- flex 左右布局-----在手机端,当左侧宽度固定,右侧宽度自适应时,右侧会超出浏览器窗口的问题解决方案
废话不多说:直接上代码解决问题: 父级: .parent { display: flex; flex-flow: row; } 子级: .left-child { width:100px; } .ri ...
- Unity编辑器批量设置图片格式
在游戏开发中,经常需要批量设置图片的格式为Sprite类型,手动设置太麻烦,下面的编辑器脚本实现选中文件夹右键/Texture/SetAllImagesToSpriteType实现批量设置图片格式,具 ...
- SCC缩点模板
struct SCC { int top = 0, cntscc = 0, dfncnt = 0, n; vector<int> dfn, low, stk, instk; vector& ...
- 瑞芯微 | 如何固定以太口地址为指定ip?ifplugd妙用
rxw的RK3568的evb1公板,有2个以太口, 默认UI界面只能配置eth0,无法配置eth1, 实际应用中,有时需要一旦有网线插入,就需要该地址设置为指定IP地址. 本文介绍2个最简单的方法实现 ...
- Devexpress 控件学习记录(一:BarManager 控件、XtraTabbedMdiManager 控件)
BarManager 控件 最终实现的效果如下: 首先在窗体中拖出BarManager控件,窗体Bar add地方点击添加 设置BarManager的属性 设置出现的窗体的底部[DockStyle=B ...
- 神奇的C语言输出12天圣诞节歌词代码
12天圣诞节程序怎样运行?1988 年,一个令人印象深刻且令人敬畏的 C 代码,代号为 xmas.c,在国际混淆 C 代码竞赛中获胜.该程序甚至比其输出的"压缩"类型还要小,代表了 ...