我想在 Jetpack compose 中为我的图标添加阴影,以便图像和文本具有(大致)相似的阴影。
Text(
text = "HAS SHADOW",
style = MaterialTheme.typography.body2.copy(
shadow = Shadow(
color = Color(0x4c000000),
offset = Offset(2f, 2f),
blurRadius = 7f
)
),
)
Text(
text = "HAS NO SHADOW",
style = MaterialTheme.typography.body2
)
Run Code Online (Sandbox Code Playgroud)
请注意:正如您在上面看到的,图标是部分透明的,我想保持这种状态 -> 像“将其包裹在 FloatingActionButton 中”这样的解决方案将不起作用
我可以在撰写中执行此操作还是必须要求我的设计师添加阴影?
我正在使用 jetpack compose 编写一个 android 应用程序。
这个应用程序有一个底部栏,我有时想使用动画隐藏它。然而,这证明是具有挑战性的:当我处理可滚动屏幕时,我的用户界面出现了一些“跳跃” - 请参阅帖子末尾。
我的最小示例如下所示:
@Preview
@Composable
fun JumpingBottomBarScreen() {
var bottomBarVisible by remember { mutableStateOf(false) }
Scaffold(
content = { padding ->
Column(
modifier = Modifier
.verticalScroll(rememberScrollState())
.fillMaxWidth()
.background(Color.LightGray)
.padding(padding)
) {
(1..20).forEach { Text(text = "Test #$it of 50") }
Button(
onClick = { bottomBarVisible = !bottomBarVisible },
content = { Text(if (bottomBarVisible) "Hide Bottom Bar" else "Show Bottom Bar") }
)
(21..50).forEach { Text(text = "Test #$it of 50") }
}
}, …Run Code Online (Sandbox Code Playgroud) android android-jetpack-compose android-jetpack-compose-scaffold
情况
我正在使用Kotlin&编写一个非常简单的应用程序Android Jetpack Compose
我有一个scaffold包含我的navHost和一个bottomBar. 我可以用它bottomBar在三个主屏幕之间导航。其中一个主屏幕有一个详细信息屏幕,该屏幕不应显示bottomBar.
我的代码
到目前为止,这是小菜一碟:
// MainActivitys onCreate
setContent {
val navController = rememberAnimatedNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route?.substringBeforeLast("/")
BottomBarNavTestTheme {
Scaffold(
bottomBar = {
if (currentRoute?.substringBeforeLast("/") == Screen.Detail.route) {
MyBottomNavigation(
navController,
currentRoute,
listOf(Screen.Dashboard, Screen.Map, Screen.Events) // main screens
)
}
}
) { innerPadding ->
NavHost( // to be replaced by AnimatedNavHost
navController = navController,
startDestination = Screen.Dashboard.route,
modifier = …Run Code Online (Sandbox Code Playgroud) 对于我的 Android 应用程序,我使用build.gradle.kts包含以下代码的文件:
android {
...
lint {
isAbortOnError = false
}
}
Run Code Online (Sandbox Code Playgroud)
对于我当前的配置,一切正常:
com.android.tools.build:gradle:7.0.4distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zipAndroid Studio 建议更新com.android.tools.build:gradle:7.1.0,这会破坏我的 gradle 构建:
org.gradle.internal.exceptions.LocationAwareException: Build file '/Users/me/Projects/extern/my-project/app/build.gradle.kts' line: 144
Script compilation error:
Line 144: isAbortOnError = false
^ Unresolved reference: isAbortOnError
1 error
at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost$compileSecondStageOf$cacheDir$1.invoke(Interpreter.kt:669)
at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost$compileSecondStageOf$cacheDir$1.invoke(Interpreter.kt:390)
at org.gradle.kotlin.dsl.provider.CompileKotlinScript.execute(KotlinScriptEvaluator.kt:376)
at org.gradle.internal.execution.steps.ExecuteStep.executeInternal(ExecuteStep.java:89)
at org.gradle.internal.execution.steps.ExecuteStep.access$000(ExecuteStep.java:40)
at org.gradle.internal.execution.steps.ExecuteStep$1.call(ExecuteStep.java:53)
at org.gradle.internal.execution.steps.ExecuteStep$1.call(ExecuteStep.java:50)
at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)
at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:199)
at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:157)
at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:73)
at …Run Code Online (Sandbox Code Playgroud) 我的客户有非常严格的设计准则,例如对于 alpha:\n文本不应有 alpha。
\n但是,我似乎无法以类似主题的方式自定义 MaterialDesign\xc2\xb4s alpha 。
\n当然,我可以自定义每个组件,但这很乏味:\nModifier.alpha(1.0f)
当然,我可以将整个应用程序包装在 alpha-Provider 中,但大多数材料组件会覆盖它,例如AppBar,所以这也不能按预期工作。
// my wrapper:\nCompositionLocalProvider(LocalContentAlpha provides 1.0f) { /* my ui */ }\n\n// will be overwritten by \n@Composable\nprivate fun AppBar(...) {\n Surface(...) {\n CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {\n Row(..., content)\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n所以我想我应该做的是提供一个 custom ContentAlpha,因为它几乎用于每个材质组件,但我不知道如何做。你?
我收到 Kotlin 错误:
“任意表达式的单位转换”功能是实验性的,应明确启用。您还可以将此表达式的原始类型更改为 (...) -> Unit
我的代码如下:
val foo: () -> String = { "Test" }
fun bar(doSometing: () -> Unit) { /* */ }
val baz = bar(foo) // here foo throws the error
Run Code Online (Sandbox Code Playgroud)
很明显我做错了什么:bar期望() -> Unit,但我提供() -> String。
但是,错误消息意味着我可以选择“任意表达式的单位转换”。我该怎么做呢?
我在这里找到的唯一相关的东西并没有回答我的问题:/sf/ask/5056683761/
我正在寻找一种将 a 过滤List<Pair<T?, U?>>为 a 的方法List<Pair<T, U>>:我想忽略包含 null 的对。
有效,但我也需要智能广播:
fun <T, U> List<Pair<T?, U?>>.filterAnyNulls() =
filter { it.first != null && it.second != null }
Run Code Online (Sandbox Code Playgroud)
可以用,但是很难看!!,而且一点也不符合习惯
fun <T, U> List<Pair<T?, U?>>.filterAnyNulls() = mapNotNull {
if (it.first == null || it.second == null) {
null
} else {
Pair(it.first!!, it.second!!)
}
}
Run Code Online (Sandbox Code Playgroud)
有什么好的解决方案吗?还有更通用的解决方案吗?(甚至可能在可以解构的类中,例如三元组或数据类?)
KotlinsSortedMap是“一个进一步提供其键上的总排序的映射”。
因此,它应该是可索引的。但是这个扩展不存在
`sortedMap.forEachIndexed()`
Run Code Online (Sandbox Code Playgroud)
为什么不?我是否忽略了什么?是性能原因吗?没有人打扰吗?
(是的,我知道,我可以使用 List<Pair<Key, Value>>,但这对于我的用例来说并不像“直观”结构,地图更适合)
我想在撰写中测试一个简单的布局:
ConstraintLayout(黄色)包装
我是这样实现的:
@Composable
@Preview
fun MapOverlay() {
ConstraintLayout(
modifier = Modifier
.background(Color.Yellow)
.fillMaxHeight()
) {
val (stickyTop, scroller, stickyBottom) = createRefs()
Text(text = "Sticky Top Text",
modifier = Modifier
.constrainAs(stickyTop) {
top.linkTo(parent.top)
}
.background(Color.Green)
)
Column(
modifier = Modifier
.constrainAs(scroller) {
top.linkTo(stickyTop.bottom)
bottom.linkTo(stickyBottom.top)
height = Dimension.fillToConstraints
}
.verticalScroll(rememberScrollState())
) {
repeat(80) {
Text(
text = "This is Test $it of 80",
modifier = Modifier
.fillMaxWidth()
.background(Color.LightGray)
)
}
}
Text(text = "Sticky Bottom Text",
modifier = …Run Code Online (Sandbox Code Playgroud) 让我们假设以下 when 语句:
when(a)
{
x -> doNothing()
y -> doSomething()
else -> doSomethingElse()
}
Run Code Online (Sandbox Code Playgroud)
现在我正在寻找消除样板功能“doNothing()”,例如:
x -> //doesn't compile
x -> null //Android Studio warning: Expression is unused
x -> {} //does work, but my corporate codestyle places each '{‘ in a new line, looking terrible
//also, what is this actually doing?
Run Code Online (Sandbox Code Playgroud)
有什么更好的想法吗?我不能完全消除x ->,因为那会导致else -> doSthElse()
编辑:在写完这个问题之后,我想出了一个可能的答案x -> Unit。那有什么缺点吗?
我目前有一个用 jetpack compose 编写的应用程序,它使用androidx.compose.material:material.
from / import androidx.compose.material.MaterialTheme
@Composable
fun MaterialTheme(
colors: Colors = MaterialTheme.colors,
typography: Typography = MaterialTheme.typography,
shapes: Shapes = MaterialTheme.shapes,
content: @Composable () -> Unit
)
Run Code Online (Sandbox Code Playgroud)
我现在计划迁移到 Material3:(androidx.compose.material3:material3我知道仍处于 alpha 版本)。
但是,主题可组合现在不再允许任何形状
from / import androidx.compose.material3.MaterialTheme
@Composable
fun MaterialTheme(
colorScheme: ColorScheme = MaterialTheme.colorScheme,
typography: Typography = MaterialTheme.typography,
content: @Composable () -> Unit
)
Run Code Online (Sandbox Code Playgroud)
我现在应该如何处理旧的形状定义?材料网站只讨论如何在 xml 和旧视图系统中做到这一点。
material-design material-components-android android-jetpack-compose android-jetpack-compose-material3
我正在构建一个可组合项,例如:
@Composable
fun BottomSectionWrapper(
content: @Composable () -> Unit,
bottomSection: @Composable () -> Unit,
)
Run Code Online (Sandbox Code Playgroud)
我现在想知道参数是否bottomSection为空,以便我可以相应地调整我的布局。这可能吗?
我不会再进一步定义“空”;我会采取技术上可行的一切:
让我解释一下我的典型用例:我使用这个包装器将按钮附加到屏幕的下端。如果有一个按钮,我想添加淡入淡出。
我尝试了什么?
我尝试使 lambda 可为空 - 但未编译
BottomSectionWrapper(
content = { /* ... */ },
bottomSection = if(showDeleteButton){
Button { Text("Delete") }
} else null
)
Run Code Online (Sandbox Code Playgroud)
另外:直观上,我会这样写(简单的空检查不会检测到):
BottomSectionWrapper(
content = { /* ... */ },
bottomSection = {
if (showDeleteButton) {
Button { Text("Delete") }
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个字符串,例如“mon”或“tue”或“sun”。我想将其解析为 a DayOfWeek(如果不成功则为 null)。
我正在使用 Kotlin,但我想 Java 人员也会明白我想做什么:
private fun String.parseToDayOfWeek(pattern: String = "EE") =
try {
DateTimeFormatter.ofPattern(pattern, Locale.US).parse(this, DayOfWeek::from)
} catch (e: Exception){
null
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,我只是得到nulls。
相反,在我解析它之前,我必须像这样清理字符串:
val capitalized = this.lowercase().replaceFirstChar { it.uppercase() }
Run Code Online (Sandbox Code Playgroud)
这感觉很麻烦。我使用的 api 是错误的还是这是一个巨大的绊脚石?
android ×6
kotlin ×5
android-jetpack-compose-material3 ×1
android-jetpack-compose-scaffold ×1
collections ×1
date-parsing ×1
dayofweek ×1
dictionary ×1
gradle ×1
java ×1
java-time ×1
jetpack-compose-accompanist ×1
material-components-android ×1
nullable ×1
sortedmap ×1