关于这个 Android Jetpack Compose 的学习,我觉得看视频学习比看书学习要更加高效。因此,我决定跟着 YT 上面的一个视频系列把这个基础先过一遍,并且在这里记录以下学习笔记。
教程先看一遍,然后也结合着官方的文档教程来进行笔记的整理。
以问题的方式来记录学习过程似乎也还行。
Create First Jetpack Compose App
中规中矩,创建一个新的 Empty Compose 项目即可。
Rows, Columns and Basic Sizing
如果直接铺陈组件,那么组件之间会重叠,
1 2 3 4 5 6 7
| @Composable fun ArtistCard() { Column { Text("Alfred Sisley") Text("3 minutes ago") } }
|
使用 Row
和 Column
可以来调整布局,
1 2 3 4 5 6 7 8 9 10
| @Composable fun ArtistCard(artist: Artist) { Row(verticalAlignment = Alignment.CenterVertically) { Image() Column { Text(artist.name) Text(artist.lastSeenOnline) } } }
|
备份:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| package com.fan.basicscodelab
import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.animation.core.Spring import androidx.compose.animation.core.animateDpAsState import androidx.compose.animation.core.spring import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.fan.basicscodelab.ui.theme.BasicsCodelabTheme
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { BasicsCodelabTheme { MyApp() } } } }
@Composable fun Greeting(name: String) { var expanded by remember { mutableStateOf(false) }
val extraPadding by animateDpAsState( if (expanded) 48.dp else 0.dp, animationSpec = spring( dampingRatio = Spring.DampingRatioMediumBouncy, stiffness = Spring.StiffnessLow ) )
Surface( color = MaterialTheme.colors.primary, modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp) ) { Row(modifier = Modifier.padding(24.dp)) { Column(modifier = Modifier .weight(1f) .padding(bottom = extraPadding.coerceAtLeast(0.dp)) ) { Text(text = "Hello, ") Text(text = name, style = MaterialTheme.typography.h4.copy( fontWeight = FontWeight.ExtraBold )) } OutlinedButton( onClick = { expanded = !expanded }, ) { Text(if (expanded){ stringResource(R.string.show_less) } else { stringResource(R.string.show_more) }) } } } }
@Composable fun MyApp() { var shouldShowOnboarding by rememberSaveable { mutableStateOf(true) }
if (shouldShowOnboarding) { OnboardingScreen(onContinueClicked = { shouldShowOnboarding = false }) } else { Greetings() } }
@Composable private fun Greetings(names: List<String> = List(1000) { "$it" }) { LazyColumn(modifier = Modifier.padding(vertical = 4.dp)) { items(items = names) { name -> Greeting(name = name)} } }
@Composable fun OnboardingScreen(onContinueClicked: () -> Unit) {
Surface { Column( modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { Text(text = "Welcome to the Basics Codelab!") Button( modifier = Modifier.padding(vertical = 24.dp), onClick = onContinueClicked ) { Text(text = "Continue") } } }
}
|