I know the difference between them. I want to calculate the text height
base on lineHeight
. The value of lineHeight
is in TextUnit
so I want to convert it into Dp
.
mutableStateOf creates an observable MutableState<T> , which is an observable type integrated with the compose runtime. interface MutableState<T> : State<T> { override var value: T } Any changes to value will schedule recomposition of any composable functions that read value .
A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It's similar to a Recyclerview in the classic Android View system.
Jetpack Compose is a modern declarative UI Toolkit for Android. Compose makes it easier to write and maintain your app UI by providing a declarative API that allows you to render your app UI without imperatively mutating frontend views.
You need to grab the current Density
from the LocalDensity
—so this will only work in composition, within a @Composable
function—and use that to convert to Dp
:
val lineHeightSp: TextUnit = 12.sp
val lineHeightDp: Dp = with(LocalDensity.current) {
lineHeightSp.toDp()
}
private fun Int.textDp(density: Density): TextUnit = with(density) {
[email protected]()
}
val Int.textDp: TextUnit
@Composable get() = this.textDp(density = LocalDensity.current)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With