赞
踩
最近下载了AndroidStudio4.0,桌面图标变成了橘黄色,有点小清新,给人焕然一新的感觉,加载界面也变了,简直不要太时尚,在微信公众号和掘金看到几篇博客,写得不错,于是也尝试了一下.
Jetpack Compose是用于构建本机Android UI的现代工具包。Jetpack Compose使用更少的代码,强大的工具和直观的Kotlin API简化并加速了Android上的UI开发。
地址:AndroidDevTools - Android开发工具 Android SDK下载 Android Studio下载 Gradle下载 SDK Tools下载
App的build.gradle目录: buildFeatures { // Enables Jetpack Compose for this module compose true } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" }
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.core:core-ktx:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.ui:ui-tooling:0.1.0-dev02' implementation 'androidx.ui:ui-layout:0.1.0-dev02' implementation 'androidx.ui:ui-material:0.1.0-dev02' }
buildscript { ext.kotlin_version = '1.3.60-eap-25' repositories { google() jcenter() maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' } } dependencies { classpath 'com.android.tools.build:gradle:4.0.0-alpha04' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' } } } task clean(type: Delete) { delete rootProject.buildDir }
class TestActivity : AppCompatActivity(){ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MaterialTheme { Greeting("Android") } } }
@Composable private fun Greeting(s: String) { Text(text = "Hello $s!") }
在Compose中ui布局是分层级的,元素包含在其他元素中。在Jetpack Compose中,你可以通过从其他composable
函数中调composable
函数来构建UI层次结构。
android:orientation
值为vertical
, 子元素就会垂直排列,那么,在Jetpack Compose 中,如何来实现垂直布局呢?看官方文档,使用column并添加样式,该Column()
功能使您可以垂直堆叠元素。添加Column()
到NewsStory()
功能。默认设置直接将所有子项堆叠在一起,没有间距。该列本身放在内容视图的左上角。
先看官方文档:
val image = +imageResource(R.mipmap.invitation_bg) // 放在容器中,设置大小 Container(expanded = true, height = 180.dp) { //显示图片 DrawImage(image) } // 添加垂直方向间距20dp HeightSpacer(height = 20.dp)
Shape
是Material Design 系统中的支柱之一,我们来用clip
函数对图片进行圆角裁剪。
通过Compose,可以轻松利用材料设计原则。应用于 MaterialTheme()
您创建的组件。还可以给Text添加多行显示等等.
DrawerButton( R.mipmap.invitation_bg.also { }, label = "Home", isSelected = true ) { }
@Composable private fun DrawerButton( @DrawableRes icon: Int, label: String, isSelected: Boolean, action: () -> Unit ) { val textIconColor = if (isSelected) { +themeColor { primary } } else { (+themeColor { onSurface }).copy(alpha = 0.6f) } val backgroundColor = if (isSelected) { (+themeColor { primary }).copy(alpha = 0.12f) } else { +themeColor { surface } } Padding(left = 8.dp, top = 8.dp, right = 8.dp) { Surface( color = backgroundColor, shape = RoundedCornerShape(4.dp) ) { Button(onClick = action, style = TextButtonStyle()) { Row( mainAxisSize = LayoutSize.Expand, crossAxisAlignment = CrossAxisAlignment.Center ) { WidthSpacer(16.dp) Text( text = label, style = (+themeTextStyle { body2 }).copy( color = textIconColor ) ) } } } } }
/** * 作者: njb * 时间: 2018/11/26 13:30 * 描述: JetPack尝鲜 * 来源: */ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { NewsComposeText("Android JetPack Demo") } } @Composable private fun NewsComposeText(s: String) { val image = +imageResource(R.mipmap.invitation_bg) MaterialTheme() { Column( crossAxisSize = LayoutSize.Expand, modifier = Spacing(16.dp) ) { // 放在容器中,设置大小 Container(expanded = true, height = 180.dp) { Clip(shape = RoundedCornerShape(10.dp)) { //显示图片 DrawImage(image) } } // 添加垂直方向间距20dp HeightSpacer(height = 20.dp) Text(text = "Hello $s!") Text( "我是来尝鲜 JetPack Compose 的!JetPack Compose 尝鲜 很爽,JetPack Compose 复用性又强,可以抽取很多公共组件", maxLines = 2, overflow = TextOverflow.Ellipsis, style = (+themeTextStyle { h5 }).withOpacity(0.5f) ) Text("Android JetPack 尝鲜", style = (+themeTextStyle { body1 }).withOpacity(0.5f)) Text("不要怂就是干", style = (+themeTextStyle { body2 }).withOpacity(0.6f)) DrawerButton( R.mipmap.invitation_bg.also { }, label = "Home", isSelected = true ) { } } } } } @Composable private fun DrawerButton( @DrawableRes icon: Int, label: String, isSelected: Boolean, action: () -> Unit ) { val textIconColor = if (isSelected) { +themeColor { primary } } else { (+themeColor { onSurface }).copy(alpha = 0.6f) } val backgroundColor = if (isSelected) { (+themeColor { primary }).copy(alpha = 0.12f) } else { +themeColor { surface } } Padding(left = 8.dp, top = 8.dp, right = 8.dp) { Surface( color = backgroundColor, shape = RoundedCornerShape(4.dp) ) { Button(onClick = action, style = TextButtonStyle()) { Row( mainAxisSize = LayoutSize.Expand, crossAxisAlignment = CrossAxisAlignment.Center ) { WidthSpacer(16.dp) Text( text = label, style = (+themeTextStyle { body2 }).copy( color = textIconColor ) ) } } } } }
原创|Android Jetpack Compose 最全上手指南 - 掘金
官方网站地址:Android Compose 教程 | Android 开发者 | Android Developers
腾讯视频链接: v.qq.com/x/page/n301…
Bilibili 视频链接 www.bilibili.com/video/av738…
JetPackComposeDemo: Jetpack compose使用简单实例,包括文字、图片、按钮等
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。