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()
}
随机推荐
- 【转转】 Huber Loss
原文地址: https://www.cnblogs.com/nowgood/p/Huber-Loss.html ============================================ ...
- 2.6倍!WhaleTunnel 客户POC实景对弈DataX
作为阿里早期的开源产品,DataX是一款非常优秀的数据集成工具,普遍被用于多个数据源之间的批量同步,包括类似Apache DolphinScheduler的Task类型也对DataX进行了适配和增强, ...
- AT Educational DP Contest
https://atcoder.jp/contests/dp J - Sushi 设 \(f[i,j,k]\) 表示有 \(1/2/3\) 个寿司的盘子有 \(i/j/k\) 个 考虑随机到哪种盘子列 ...
- C++开发分类
1.基础架构 2.音视频领域 3.安全方向 4.Linux虚拟化 5.Qt客户端.上位机 6.游戏领域 7.嵌入式 8.量化券商 暂时对基础架构.音视频和安全方向较为感兴趣.
- Kubernetes 初学部署遇到的问题
### Kubernetes 部署文档(CentOS 7.9) 本文采用centos9 截至2024年8月21日官网已经不再提供其他下载 直接用最新版即可 1. CentOS 7.9 内核版本问题 截 ...
- stm32学习之调试篇踩坑记录
如何下载fml文件 找不到64k的fml文件 could not stop cortex-m device:无法连接,我当时是在程序中的一些初始化函数中,有几条语句禁用了(JTAG+SW) 接线顺序, ...
- pyinstaller 打包 win32ctypes.pywin32.pywintypes.error: (225, '', '无法成功完成操作,因为文件包含病毒或潜在的垃圾软件。')
背景: 使用python 写了一个程序,使用pyinstaller打包, 不使用-w --noconsole的命令打包隐藏命令行窗口时,是正常的, 但是使用-w或者--noconsole就会报错win ...
- 进行中 10% 手机Root安装银河麒麟ARM系统,或其它的CentOS系统
计划中: 预计开始时间: 最早开始3/12 预计结束时间:最早3/13号 计划过程: 1. 手机root 2. 安装系统 目前:1. 我的旧旧旧手机,是红米note5,然后我进了官网,想要解锁, 解锁 ...
- 示例python 批量操作excel统计销售榜品牌及销售额
示例统计销售榜品牌及销售额 import pandas as pd import numpy as np import os os.chdir('F:\\50mat\源数据1000张表格') name ...
- Playwright 源码 BrowserType
playwright-java 的 Browser.BrowserContext.Page 挺好理解的,唯独这厮,就有一丢丢 -- package com.microsoft.playwright; ...