Ниже приведен код, в котором навигация по ссылкам не работает. Здесь обычный контент преобразуется в HTML, расширяемый контент обнаруживает гиперссылку и поддерживает событие щелчка для навигации по URL-адресу, доступному для просмотра.
В этом коде HTML-содержимое не отображается должным образом. когда текст отображается, он отображается как обычный текст без какого-либо эффекта.
@Composable
fun AccordionView(
titleHtml: String,
descriptionHtml: String,
onLinkClick: (String) -> Unit = {}
) {
var expanded by remember { mutableStateOf(false) }
val rotation by animateFloatAsState(if (expanded) 180f else 0f, label = "arrowRotation")
Column(modifier = Modifier.padding(12.dp)) {
Row(
modifier = Modifier
.fillMaxWidth()
.clickable { expanded = !expanded },
verticalAlignment = Alignment.CenterVertically
) {
HtmlText(titleHtml, onLinkClick = onLinkClick)
Icon(
painter = painterResource(id = android.R.drawable.arrow_down_float),
contentDescription = null,
modifier = Modifier.rotate(rotation)
)
}
AnimatedVisibility(expanded) {
HtmlText(descriptionHtml, onLinkClick = onLinkClick)
}
}
}
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.core.text.HtmlCompat
@Composable
fun HtmlText(
html: String,
onLinkClick: (String) -> Unit = {}
) {
val spanned = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY)
val annotated = spannedToAnnotatedString(spanned)
SelectionContainer { // allows text selection
Text(
text = annotated,
style = MaterialTheme.typography.bodyMedium,
modifier = Modifier.pointerInput(Unit) {
detectTapGestures { offset ->
annotated.getStringAnnotations("URL", offset, offset)
.firstOrNull()?.let { onLinkClick(it.item) }
}
}
)
}
}
import android.text.Spanned
import android.text.style.StyleSpan
import android.text.style.URLSpan
import android.text.style.BulletSpan
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.core.text.HtmlCompat
fun spannedToAnnotatedString(spanned: Spanned): AnnotatedString {
return buildAnnotatedString {
append(spanned.toString())
// Bold / Italic
spanned.getSpans(0, spanned.length, StyleSpan::class.java).forEach { span ->
val start = spanned.getSpanStart(span)
val end = spanned.getSpanEnd(span)
when (span.style) {
android.graphics.Typeface.BOLD -> addStyle(
SpanStyle(fontWeight = FontWeight.Bold), start, end
)
android.graphics.Typeface.ITALIC -> addStyle(
SpanStyle(fontStyle = FontStyle.Italic), start, end
)
}
}
// Links
spanned.getSpans(0, spanned.length, URLSpan::class.java).forEach { span ->
val start = spanned.getSpanStart(span)
val end = spanned.getSpanEnd(span)
addStyle(
SpanStyle(
color = MaterialTheme.colorScheme.primary,
textDecoration = TextDecoration.Underline
),
start, end
)
addStringAnnotation("URL", span.url, start, end)
}
// Bullets
spanned.getSpans(0, spanned.length, BulletSpan::class.java).forEach { span ->
val start = spanned.getSpanStart(span)
insert(start, "• ")
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... destinatio