如何设置 Row 中元素的重力?我的行中有两个图像可组合项。我希望第一个图像位于行的开头,下一个元素位于行的末尾。
我尝试过这样做:
Row(Modifier.fillMaxWidth()) {
Image(
painter = painterResource(id = R.drawable.logo_voodlee),
contentDescription = "logo",
modifier = Modifier
.width(with(LocalDensity.current) { dimensionResource(id = R.dimen._100sdp) })
.height(with(LocalDensity.current) { dimensionResource(id = R.dimen._55sdp) })
.offset(x = with(LocalDensity.current) { dimensionResource(id = R.dimen._16sdp) }),
)
Image(
painter = painterResource(id = R.drawable.ic_menu),
contentDescription = "logo",
modifier = Modifier
.width(with(LocalDensity.current) { dimensionResource(id = R.dimen._19sdp) })
.height(with(LocalDensity.current) { dimensionResource(id = R.dimen._19sdp) })
,
alignment = CenterEnd //This is not working
)
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用。
我正在使用 Jetpack Compose 来实现绘制“逐渐增长的线”的要求,即从指定的起点和点开始并逐渐“增长”直到到达指定的终点的线。我之前用自定义视图实现了这个,效果很好。我的逻辑要求根据某些条件一次又一次地重新调用“onDraw()”。为此,我在使用自定义视图时使用了“invalidate()”。但现在我正在使用 Jetpack Compose,无法找到重新组合“Canvas”的方法。
这是我的 Jetpack 为“逐渐增长的线”编写的代码:
@Composable
fun SplashUI() {
var test = remember { mutableStateOf(0) }
Canvas(modifier = Modifier.fillMaxSize()) {
// starting point
x1 = 0.0;
y1 = size.height / 2.0;
// ending point
x2 = size.width.toDouble()
y2 = size.height / 2.0;
divideLineIntoEqualParts();
if (test.value < listOfPoints.size) {
drawLine(
Color.Black,
Offset(
listOfPoints.get(0)?.x!!,
listOfPoints.get(0)?.y!!
),
Offset(
listOfPoints.get(inte)?.x!!,
listOfPoints.get(inte)?.y!!
),
strokeWidth = 5f
);
}
test.value = test.value + 1 //This doesn't trigger recomposition of canvas
//Recomposition of …
Run Code Online (Sandbox Code Playgroud) android android-canvas android-jetpack-compose android-jetpack-compose-canvas
我读到的所有关于类型具体化的教程都说我们在使用“具体化”时需要使用“内联”,但没有一个解释原因。
假设我有一个函数:
inline fun <reified T> doSomething(value: T) {
println("Doing something with type: ${T::class.simpleName}")
}
Run Code Online (Sandbox Code Playgroud)
据我了解,使用“具体化”可以防止类型擦除。那么为什么我们不能在普通的非内联函数中使用它呢?使用内联将使编译器在调用站点复制上述函数的主体。但为什么我们需要这样的事情发生呢?
generics inline type-erasure kotlin kotlin-reified-type-parameters
我有不同的可组合项,每个可组合项代表一个屏幕。我为此使用了导航控制器。如何在从一个可组合项(屏幕)到另一个可组合项(屏幕)时添加滑入/滑出动画?
我试图将按钮的文本垂直和水平居中对齐,默认情况下不存在。我尝试使用“偏移”来定位按钮中的文本,但在各种设备尺寸上定位并不一致。
我的按钮的代码是:
Button(
onClick = {
navController.navigate("fourth_screen")
},
modifier = Modifier.constrainAs(buttonSave) {
top.linkTo(glButtonSaveTop)
bottom.linkTo(glButtonSaveBottom)
start.linkTo(glLeft)
end.linkTo(glRight)
width = Dimension.fillToConstraints
height = Dimension.fillToConstraints
},
enabled = !errorMsg.value,
colors = if (query.value.text != ""){
ButtonDefaults.
buttonColors(backgroundColor = colorResource(id = R.color.voodlee_red))}
else {
ButtonDefaults.
buttonColors(backgroundColor = colorResource(id = R.color.gray))}
) {
Text(
"Save", color = colorResource(id = R.color.dark_blue),
fontSize = with(LocalDensity.current) {
dimensionResource(id = R.dimen._16ssp).toSp()
},
fontFamily = FontFamily(Font(R.font.poppins_medium)),
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxSize().offset(y= (0.15).dp)) //offset for positioning
}
Run Code Online (Sandbox Code Playgroud)
如何使按钮中的文本垂直和水平居中,以适应所有设备尺寸。
编辑:在稳定的 Jetpack …
我试图让我的 TextField 仅接受使用keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text)
以下字符:
TextField(
value = query3.value,
onValueChange = { newValue ->
query3.value = newValue
},
singleLine = true,
label = {
Text(
"Bank name",
color = colorResource(id = R.color.bright_green),
fontFamily = FontFamily(Font(R.font.poppins_regular)),
fontSize = with(LocalDensity.current) { dimensionResource(id =
R.dimen._12ssp).toSp() },
)
},
interactionSource = interactionSource,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
.
.
.
Run Code Online (Sandbox Code Playgroud)
但软键盘仍然允许输入数字。如何让软键盘只允许输入字符?
android textfield android-softkeyboard android-jetpack-compose
android ×5
alignment ×1
android-jetpack-compose-canvas ×1
button ×1
generics ×1
inline ×1
kotlin ×1
kotlin-reified-type-parameters ×1
row ×1
textfield ×1
type-erasure ×1