文本将重叠如果我在Jetpack Compose中使用主题。

huangapple go评论59阅读模式
英文:

Text will overlap if I use a theme in jetpack compose

问题

The issue you're experiencing with text overlapping in Jetpack Compose UI is likely due to the custom theme you've defined in your code. It seems to be altering the status bar color and appearance, which might be causing layout issues. To fix this bug and prevent text overlap, you can try the following steps:

  1. Remove the custom theme, "CalaulatorTheme," from your code and use the default theme provided by Jetpack Compose.

  2. Test your UI without the custom theme to see if the text displays correctly. If it does, then the issue is likely related to the custom theme you've implemented.

  3. If you still want to customize the theme, consider making adjustments to it gradually and testing the UI after each change to identify which part of the theme is causing the overlap issue.

  4. Ensure that your theme modifications do not interfere with the layout of the Text composable inside your Column.

By following these steps, you should be able to identify and resolve the text overlap issue in your Jetpack Compose UI.

英文:

I write an app with jetpack compose UI. that is only a Text compose in my screen. But I found that the text will overlap if I use a default theme. How can I Solve this problem?

The UI is like this :

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
        setContent {
            CalaulatorTheme {
                // A surface container using the 'background' color from the theme
              
                Box(modifier = Modifier.fillMaxSize().background(MediumGray).padding(16.dp)) {
                    Column(
                        modifier = Modifier
                            .fillMaxWidth()
                            .align(Alignment.BottomCenter),
                        verticalArrangement = Arrangement.spacedBy(8.dp)
                    ) {
                        Text(
                            text = "12345678901233445568256",
                            textAlign = TextAlign.End,
                            modifier = Modifier
                                .fillMaxSize()
                                .background(Color.Magenta)
                                .padding(vertical = 8.dp),
                            fontWeight = FontWeight.Light,
                            fontSize = 60.sp,
                            color = Color.White,
                            maxLines = 2
                        )
                    }
                }
            }
        }
    }
}

the default Theme is like this:

@Composable
fun CalaulatorTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            (view.context as Activity).window.statusBarColor = colorScheme.primary.toArgb()
            ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme
        }
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}

But the result is like this:

文本将重叠如果我在Jetpack Compose中使用主题。

Why do the texts overlap with each other? How should I fix this bug?
if I remove the "CalaulatorTheme", the text display normal.

答案1

得分: 2

Replace your fontSize = 60.sp with style = TextStyle(fontSize = 60.sp).

文本将重叠如果我在Jetpack Compose中使用主题。

英文:

Just replace your fontSize = 60.sp with style = TextStyle(fontSize = 60.sp)

文本将重叠如果我在Jetpack Compose中使用主题。

huangapple
  • 本文由 发表于 2023年5月7日 17:43:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定