组合函数

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()
}

随机推荐

  1. 【转载】 IEEE Signal Processing Letters(SPL)投稿经验分享

    版权声明:本文为CSDN博主「yellow7-」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/weixin_4 ...

  2. Apache DolphinScheduler PMC:开源不一定也要九死一生

    点亮 ️ Star · 照亮开源之路 GitHub:https://github.com/apache/dolphinscheduler   ​   参与开源已经快3年了,这次在Meetup上没有分享 ...

  3. 解决input自动填充账号密码时的背景色

      input:-webkit-autofill { box-shadow:0 0 0 1000px white inset !important; } input:-internal-autofil ...

  4. CentOS下离线安装gcc环境,图文详细,方法全面

    CentOS下离线安装gcc环境,图文详细,方法全面 下载 方式1:如果有网的虚拟机还没有安装,可以直接 yum install --downloadonly --downloaddir=/root/ ...

  5. java-多线程(下)

    多线程简单入门(Java)(下篇:多线程Java中的使用) 目录 一.创建多线程 二.线程的安全 三.线程的通信 一.创建多线程 在Java中,多线程的创建有4种方式. 方式一:继承于Thread类; ...

  6. SMU 2024 spring 天梯赛自主训练2

    SMU 2024 spring 天梯赛自主训练2 7-1 I Love GPLT - SMU 2024 spring 天梯赛自主训练2 (pintia.cn) PHP 点击查看代码 I L o v e ...

  7. rk3568 | rk平台GPIO冲突检测小技巧

    上一篇我们讲解了如何编写gpio驱动,但是实际操作中,经常发现gpio引脚被占用的情况发生,那么本篇文章就详细讲解rxw平台下如何快速定位gpio复用问题以及如何解决. 一.GPIO寄存器查找 要想查 ...

  8. 基于surging的产品项目-木舟开源了!

    一 . 概述 因为前段时间电脑坏了,导致代码遗失,踌躇满志马上上线的平台产品付之东流,现在熬夜在写代码希望能尽快推出企业正常使用的平台产品,而这次把代码开源,一是让大家对surging 使用有个深入的 ...

  9. stm32f10x系列学习笔记(三)ADC

    ADC_SampleTime 用途:在ADC通道配置(ADC_RegularChannelConfig)需要传输的参数. 含义:指两个采样阶段之间的延迟周期数,该参数会影响ADC在采样过程中的性能和准 ...

  10. python的命名风格(下划线篇)

    一个下划线开头的代表模块私有 用from xxx import * 时python会自动屏蔽带下划线的东西,想要取消屏蔽可以用__all__方法,但不建议(不符合规范) 两个下划线开头的代表类私有