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()
}
随机推荐
- 【转载】 从零开始编写一个简单的Linux文件系统
版权声明:本文为CSDN博主「shuxiaogd」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/shuxiao ...
- python库 —— gym retro 的 ROMs文件下载地址
如题: python库 -- gym retro 的 ROMs文件下载地址: https://archive.org/details/No-Intro-Collection_2016-01-03_Fi ...
- Functional PHP (通义千问)
Functional PHP 是一个专为 PHP 开发者设计的库,旨在引入函数式编程的概念和工具,帮助开发者编写更高效.可读性强的代码.以下是几个使用 Functional PHP 库进行函数式编程的 ...
- Java基础之数值类型之间的转换
经常需要将一种数值类型转换为另一种数值类型.下图 给出了数值类型之间的合法 转换. 在图中有 6 个实心箭头,表示无信息丢失的转换:有 3 个虚箭头, 表示可能有精度 损失的转换. 例如,123 45 ...
- Vim 全局配置 / 设置鼠标模式
新搞的 Linux (Debian) 上的 vim 一右击粘贴就变成 insert (Visual) 模式,上网查了一下,要 set mouse=,但是每次设置太麻烦了,另外我也想改一下全局配色. 定 ...
- 100ASK_IMX6ULL arm板子如何移植刷卡器
最近为了在arm板子上移植刷卡器,比较简单,但也遇到了坑,现在和大家分享下. 购买刷卡器 某宝很多,应该选哪一种呢? 一口君一共买了4种刷卡器,有2种可以用,还有2种不能用. 下图为最方便的一款,一口 ...
- Linux内核信号SIGIO使用实例讲解
一.信号 1. 基本概念 信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是异步的,一个进程不必通过任何操作来等待信号的到达,事实上, ...
- zabbix资产清单inventory管理
概述 监控的设备越来越多,有时候搞不清楚哪台服务器是什么配置,大多公司有自己的资产清单,要去专门的系统查询显得多少有点麻烦.为此,zabbix专门设置了设备资产管理功能.我们创建或者编辑主机的时候,可 ...
- macOS 磁盘设备文件命名规则
macOS 系统使用不同于 Linux 的磁盘设备命名规则.在 macOS 中,磁盘设备和分区被命名并通过 /dev 目录访问,类似于 Linux 和 UNIX 系统.但是,macOS的命名规则遵循特 ...
- Zabbix-(1)安装
环境: VMware Workstation Pro 16.0 版本 系统 Centos7 内存 2G 处理器 1G 硬盘 20G 网络适配器 NAT 服务器地址:192.168.220.40 1.安 ...