16
16
17
17
package com.wintmain.foundation.ui.compose
18
18
19
+ import android.os.Build.VERSION_CODES
19
20
import android.os.Bundle
21
+ import android.view.WindowInsetsController
20
22
import androidx.activity.ComponentActivity
21
23
import androidx.activity.compose.setContent
22
24
import androidx.activity.enableEdgeToEdge
25
+ import androidx.annotation.RequiresApi
26
+ import androidx.compose.foundation.Image
27
+ import androidx.compose.foundation.layout.Arrangement
28
+ import androidx.compose.foundation.layout.Box
29
+ import androidx.compose.foundation.layout.Column
23
30
import androidx.compose.foundation.layout.fillMaxSize
24
31
import androidx.compose.foundation.layout.padding
25
- import androidx.compose.material3.Scaffold
32
+ import androidx.compose.material3.MaterialTheme
33
+ import androidx.compose.material3.Surface
26
34
import androidx.compose.material3.Text
27
35
import androidx.compose.runtime.Composable
36
+ import androidx.compose.ui.Alignment
28
37
import androidx.compose.ui.Modifier
38
+ import androidx.compose.ui.layout.ContentScale
39
+ import androidx.compose.ui.res.painterResource
40
+ import androidx.compose.ui.text.style.TextAlign
29
41
import androidx.compose.ui.tooling.preview.Preview
42
+ import androidx.compose.ui.unit.dp
43
+ import androidx.compose.ui.unit.sp
44
+ import androidx.core.view.WindowInsetsCompat
30
45
import com.google.android.catalog.framework.annotations.Sample
46
+ import com.wintmain.foundation.R
31
47
32
48
@Sample(
33
49
name = " Compose Activity" ,
@@ -36,34 +52,91 @@ import com.google.android.catalog.framework.annotations.Sample
36
52
tags = [" android-samples" , " user-interface" ],
37
53
)
38
54
class ComposeActivity : ComponentActivity () {
55
+ @RequiresApi(VERSION_CODES .R )
39
56
override fun onCreate (savedInstanceState : Bundle ? ) {
40
57
super .onCreate(savedInstanceState)
41
58
enableEdgeToEdge()
59
+ window.insetsController?.apply {
60
+ // 设置window的一些参数可以调整状态栏和导航栏的显示
61
+ hide(WindowInsetsCompat .Type .systemBars())
62
+ systemBarsBehavior = WindowInsetsController .BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
63
+ }
42
64
setContent {
43
65
Android_KickoffTheme {
44
- Scaffold (modifier = Modifier .fillMaxSize()) { innerPadding ->
45
- Greeting (
46
- name = " Android" ,
47
- modifier = Modifier .padding(innerPadding)
66
+ Surface (
67
+ modifier = Modifier .fillMaxSize(),
68
+ color = MaterialTheme .colorScheme.background
69
+ ) {
70
+ // Greeting(
71
+ // message = "Happy Birthday!",
72
+ // from = "From wintmain",
73
+ // modifier = Modifier.padding(8.dp)
74
+ // )
75
+ GreetingImage (
76
+ message = " Happy Birthday" ,
77
+ from = " From wintmain" ,
78
+ modifier = Modifier .padding(8 .dp)
48
79
)
49
80
}
50
81
}
51
82
}
52
83
}
53
84
54
85
@Composable
55
- fun Greeting (name : String , modifier : Modifier = Modifier ) {
56
- Text (
57
- text = " Hello $name ! " ,
86
+ fun Greeting (message : String , from : String , modifier : Modifier = Modifier ) {
87
+ Column (
88
+ verticalArrangement = Arrangement . Center ,
58
89
modifier = modifier
59
- )
90
+ ) {
91
+ Text (
92
+ text = message,
93
+ fontSize = 60 .sp,
94
+ lineHeight = 60 .sp,
95
+ textAlign = TextAlign .Center ,
96
+ modifier = Modifier .align(alignment = Alignment .CenterHorizontally )
97
+ )
98
+ Text (
99
+ text = from,
100
+ fontSize = 24 .sp,
101
+ modifier = Modifier
102
+ .padding(12 .dp)
103
+ .align(alignment = Alignment .End )
104
+ )
105
+ }
106
+ }
107
+
108
+ @Composable
109
+ fun GreetingImage (message : String , from : String , modifier : Modifier = Modifier ) {
110
+ val image = painterResource(R .drawable.androidparty)
111
+ Box (modifier) {
112
+ Image (
113
+ painter = image,
114
+ contentDescription = null ,
115
+ contentScale = ContentScale .Crop ,
116
+ alpha = 0.5f
117
+ )
118
+ Greeting (
119
+ message = message,
120
+ from = from,
121
+ modifier = Modifier
122
+ .fillMaxSize()
123
+ .padding(8 .dp)
124
+ )
125
+ }
60
126
}
61
127
62
128
@Preview(showBackground = true )
63
129
@Composable
64
130
fun GreetingPreview () {
65
131
Android_KickoffTheme {
66
- Greeting (" wintmain" )
132
+ // Greeting(
133
+ // message = "Happy Birthday",
134
+ // from = "from wintmain"
135
+ // )
136
+ GreetingImage (
137
+ message = " Happy Birthday" ,
138
+ from = " from wintmain"
139
+ )
67
140
}
68
141
}
69
142
}
0 commit comments