100.dp如何在特定的盒子/容器(即 Size )内放置文本
有没有办法在 Compose 中将字符串放入 Box 中?
这是我尝试过的:
@Composable
fun playText() {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color(0xFF242E38)),
contentAlignment = Alignment.Center
) {
Row(modifier = Modifier
.background(Color(0xFFEEEEEE))
.width(100.dp)
.height(100.dp)
) {
Text(
text = "FIT ME",
modifier = Modifier
.fillMaxSize()
.wrapContentSize(
Alignment.Center,
unbounded = true
)
.requiredSize(100.dp),
color = Color(0xFF242E38),
fontSize = 50.sp,
style = TextStyle.Default.copy(
background = Color(0xFFEAC43D)
)
)
}
}
}
@Preview(showBackground = true)
@Composable
fun show() {
playText()
}
Run Code Online (Sandbox Code Playgroud)
这是当前结果:
预期结果如下 ( which I …
举一个简单的例子,如何在不使用 ViewModel 或 Hilt 等的情况下访问我们应用程序的 Exit 事件?
例如,当我们退出应用程序时显示简单的 Toast 消息。
下面的代码,当我们按后退按钮退出时,可以正常工作,并显示 toast:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var ctx = applicationContext
setContent {
checkExit(ctx)
}
}
}
@Composable
fun checkExit(ctx: Context) {
DisposableEffect(""){
onDispose {
Toast.makeText(ctx, "onExit", Toast.LENGTH_LONG).show()
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是如果我们最小化应用程序,然后在后台向上滑动屏幕退出,这个toast将不再显示
**Working Code, thanks to AgentP**
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var ctx = applicationContext
setContent {
val lifecycle: LifecycleOwner = LocalLifecycleOwner.current
checkExit(ctx, lifecycle)
} …Run Code Online (Sandbox Code Playgroud)