简体   繁体   English

Jetpack Compose Text 中的“.(”会导致换行

[英]“.(” in Jetpack Compose Text causes newline

In this Jetpack Compose code the result of the first Text composable has the parenthesized text on a new line:在这个 Jetpack Compose 代码中,第一个 Text 可组合的结果在新行上有带括号的文本:

Column {
    Text(text = "2.(0123456789)",
            modifier = Modifier
                    .width(60.dp)
                    .padding(start = 5.dp))
    Spacer(modifier = Modifier . padding(20.dp))
    Text(text = "2.0123456789",
            modifier = Modifier
                    .width(60.dp)
                    .padding(start = 5.dp))
}

在此处输入图像描述

This does not happen if I remove the ".", or if I remove the "(".如果我删除“。”或删除“(”,则不会发生这种情况。

The issue is modifier = Modifier.width(60.dp) which applies a fixed width to your Text .问题是modifier = Modifier.width(60.dp)将固定宽度应用于您的Text

Use Modifier.width(IntrinsicSize.Max) in your Column在您的Column中使用Modifier.width(IntrinsicSize.Max)

Column(Modifier.width(IntrinsicSize.Max)) {
    Text(text = "2.(0123456789)",
        modifier = Modifier
            .padding(start = 5.dp))
    Spacer(modifier = Modifier . padding(20.dp))
    Text(text = "2.0123456789",
        modifier = Modifier
            .padding(start = 5.dp))
}

在此处输入图像描述

If you want to specify a limited width you can use Modifier.requiredWidthIn to specify a width between mindp and maxdp and maxLines = 1 in the Text .如果你想指定一个有限的宽度,你可以使用Modifier.requiredWidthInText中指定 mindp 和 maxdp 和maxLines = 1之间的宽度。

Something like:就像是:

Column(Modifier.requiredWidthIn(0.dp , 75.dp)) {
    Text(
        text = "2.(0123456789)",
        modifier = Modifier
            .padding(start = 5.dp),
        maxLines = 1,
        overflow = TextOverflow.Ellipsis

    )
    Spacer(modifier = Modifier.padding(20.dp))
    Text(
        text = "2.0123456789",
        modifier = Modifier
            .padding(start = 5.dp),
        maxLines = 1,
        overflow = TextOverflow.Ellipsis
    )
}

Otherwise you apply the Modifier.requiredWidthIn(0.dp, 75.dp) to the Text components instead of the Column.否则,您将Modifier.requiredWidthIn(0.dp, 75.dp)应用于文本组件而不是列。

Column(Modifier.width(IntrinsicSize.Max)) {
    Text(
        modifier = Modifier
            .requiredWidthIn(0.dp, 75.dp)
        maxLines = 1
    )
    //...
    Text(
        modifier = Modifier
            .requiredWidthIn(0.dp, 75.dp)
        maxLines = 1,
    )
}

The issue ain't with character escape.问题不在于字符转义。 It is the fact that you have applied a fixed small width and because the order of the modifiers matters, the width is further trimmed by 5dps of your padding事实上,您已经应用了一个固定的小宽度,并且由于修饰符的顺序很重要,因此宽度会被 5dps 的填充进一步修剪

EDIT: I am not exactly sure, but I think it could be because of the fact that Text inherently supports the feature where a textholder would display a single word in one line if it can.编辑:我不太确定,但我认为这可能是因为 Text 本身就支持这样一个特性,即如果可以的话,文本持有者会在一行中显示一个单词。 When you insert a special character like a period or a bracket, it considers the rest of the string to be a new sentence (grammatically), and observing it's length, calculating that it will not fit in the same line, starts a new line.当您插入一个特殊字符(如句点或括号)时,它会将字符串的 rest 视为一个新句子(语法上),并观察它的长度,计算它不适合同一行,开始一个新行。 Then the length is still bigger but there is no option but to start a new line.然后长度仍然更大,但别无选择,只能开始新的一行。 Hence, the rest of the phrases go all the way to the end, then start on s new line on reaching maximum capacity.因此,短语 go 的 rest 一直到最后,然后在达到最大容量时开始新的线路。 Try inserting just a single character after the period or the bracket.尝试在句点或括号后仅插入一个字符。 I do not think that it will insert a new line since there will be room to insert the entire statement (in this case, just a single character).我不认为它会插入一个新行,因为会有空间插入整个语句(在这种情况下,只有一个字符)。 When I started typing, I thought it was not for sure, but now, I am almost convinced that this is the case.当我开始打字时,我认为这不是确定的,但现在,我几乎确信情况确实如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM