Android Jetpack Compose中如何完整显示第二段文本内容?
问题描述
展示带标题的文本内容时,第二段文本的最后一行无法完整显示(内容被截断)。尝试将文本放入Column组件中,问题仍未解决。
以下是当前代码:
import androidx.compose.foundation.background import androidx.compose.foundation.gestures.detectTransformGestures import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.material.ExperimentalMaterialApi import androidx.compose.material.FractionalThreshold import androidx.compose.material.Text import androidx.compose.material.rememberSwipeableState import androidx.compose.material.swipeable import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.navigation.NavController import com.example.speechrecognition.Destinations import kotlin.math.absoluteValue @OptIn(ExperimentalMaterialApi::class) @Composable fun SpeechScreen1(navController: NavController) { var offsetX by remember { mutableStateOf(0f) } val swipeThreshold = 200 // Adjust this threshold as needed val maxOffsetX = 1000f // Adjust this value based on your needs Column( modifier = Modifier .fillMaxWidth() .wrapContentHeight() .padding(21.dp, top = 100.dp) .pointerInput(Unit){ detectTransformGestures { _, offset,_,_ -> offsetX +=offset.x if (offsetX.absoluteValue >=swipeThreshold){ if (offsetX>0){ navController.navigate(Destinations.speechscreen2) } offsetX =0f } } } ){ Text( text = "Please read the following text:", style = TextStyle( fontSize = 20.sp, lineHeight = 28.sp, fontFamily = FontFamily.Serif, fontWeight = FontWeight(400), color = Color(0xFF000000) ) ) Text( text = "One morning Dorothy crossed the hall of the palace and knocked on the door of another girl named Trot. When told to enter, Dorothy found that Trot had company, an old sailor-man with a wooden leg who was sitting by the open window puffing smoke from a pipe.", style = TextStyle( fontSize = 32.sp, lineHeight = 40.sp, fontFamily = FontFamily.SansSerif, fontWeight = FontWeight(300), color = Color(0xFF080808) ), modifier = Modifier .width(334.dp) .height(440.dp) .padding(top = 9.dp) ) Row(modifier = Modifier.padding(top = 27.dp)) { Row( modifier = Modifier .width(77.dp) .height(7.dp) .background(color = Color(0xFF686868)) .padding(top = 26.dp) ) { } Spacer(modifier = Modifier.width(5.dp)) Row( modifier = Modifier .width(77.dp) .height(7.dp) .background(color = Color(0xFFD9D9D9)) .padding(top = 26.dp) ) { } Spacer(modifier = Modifier.width(5.dp)) Row( modifier = Modifier .width(77.dp) .height(7.dp) .background(color = Color(0xFFD9D9D9)) .padding(top = 26.dp) ) { } Spacer(modifier = Modifier.width(5.dp)) Row( modifier = Modifier .width(77.dp) .height(7.dp) .background(color = Color(0xFFD9D9D9)) .padding(top = 26.dp) ) { } } }}
解决方案
核心问题是第二段Text组件设置了固定高度440.dp,当文本实际高度超过这个值时,最后一行会被截断。结合外层布局的限制,可通过以下方式修复:
方法1:移除固定高度限制
直接去掉Text的height(440.dp),让文本自动适配内容高度,确保所有内容完整显示:
Text( text = "One morning Dorothy crossed the hall of the palace and knocked on the door of another girl named Trot. When told to enter, Dorothy found that Trot had company, an old sailor-man with a wooden leg who was sitting by the open window puffing smoke from a pipe.", style = TextStyle( fontSize = 32.sp, lineHeight = 40.sp, fontFamily = FontFamily.SansSerif, fontWeight = FontWeight(300), color = Color(0xFF080808) ), modifier = Modifier .width(334.dp) .padding(top = 9.dp) )
方法2:添加滚动支持(适合长文本场景)
如果需要保留固定高度,同时允许用户滚动查看完整文本,可添加verticalScroll修饰符:
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.rememberScrollState // 在SpeechScreen1组件内添加滚动状态 val scrollState = rememberScrollState() Text( text = "One morning Dorothy crossed...", style = TextStyle(...), modifier = Modifier .width(334.dp) .height(440.dp) .padding(top = 9.dp) .verticalScroll(scrollState) )
方法3:优化外层Column布局
外层Column使用wrapContentHeight()可能导致内容被底部的进度条控件挤压,可改为fillMaxSize()让布局占满屏幕,确保文本有足够空间展示:
Column( modifier = Modifier .fillMaxSize() .padding(21.dp, top = 100.dp) .pointerInput(Unit){ detectTransformGestures { _, offset,_,_ -> offsetX +=offset.x if (offsetX.absoluteValue >=swipeThreshold){ if (offsetX>0){ navController.navigate(Destinations.speechscreen2) } offsetX =0f } } } )
另外注意代码中同时导入了androidx.compose.material.Text和androidx.compose.material3.Text,建议统一使用其中一个,避免API混淆。
内容的提问来源于stack exchange,提问作者vilnius19
相关产品推荐
相关产品推荐

