简体   繁体   English

Android:TextView的奇怪文本换行问题

[英]Android: strange text wrapping issue with TextView

I have a TextView in a LinearLayout. 我在LinearLayout中有一个TextView。 The textview's XML textview的XML

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:background="@color/CLOUDS"
    android:ellipsize="none"
    android:maxLines="100"
    android:minHeight="20dp"
    android:minLines="1"
    android:layout_margin="0dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:paddingTop="0dp"
    android:textSize="14sp"
    android:paddingBottom="0dp"
    android:layout_weight="10"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:singleLine="false"
    android:scrollHorizontally="false"
    android:inputType="textCapSentences|textMultiLine"
    />

错误1
On the right we see the textview set with text: "Campanario Blanco\\nMistral Ice\\nMistral" The last word is not displayed. 在右侧,我们看到textview设置文本: “Campanario Blanco \\ nMistral Ice \\ nMistral”最后一个单词未显示。

On the left we have another textview with weight 10. Both are placed within a Linear layout. 在左侧,我们有另一个权重为10的文本视图。两者都放在线性布局中。

    LinearLayout linearLayout = new LinearLayout(context.mContext);
    LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    ll.topMargin = 2;
    ll.bottomMargin = 0;
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    linearLayout.setLayoutParams(ll);
    linearLayout.setPadding(0, 0, 0, 0);
    bubble.addView(linearLayout);

Now if I change the content of the view to "Campanario Blanco\\nMistral Ice\\nMistral\\nMalibu" Now 'mistral' is visible, but the new last work 'Mailbu' is not. 现在,如果我将视图的内容更改为“Campanario Blanco \\ nMistral Ice \\ nMistral \\ nMalibu”现在可以看到'mistral',但新的最后一个作品'Mailbu'不是。

错误2

As shown the total line count is equal to the sum of the \\n but it doesn't take into account the wrapping of the first string. 如图所示,总行数等于\\ n的总和,但它没有考虑第一个字符串的包装。 If we remove the long text, it works as expected. 如果我们删除长文本,它会按预期工作。 "Mistral Ice\\nMistral\\nMalibu" “Mistral Ice \\ nMistral \\ nMalibu”

没有错误

You can have something like this, use like this 你可以有这样的东西,像这样使用

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="20dp"
     android:layout_marginRight="20dp"
     android:layout_marginLeft="20dp"
     android:padding="6dp"
     android:background="#358576"
     tools:context="com.example.yujcore7.myapplicationtextviewdemo.MainActivity">


<TextView
    android:textColor="#fff"
    android:textSize="20sp"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="Marca(s)" />

<TextView
    android:textColor="#fff"
    android:textAllCaps="true"
    android:textSize="16sp"
    android:layout_marginRight="10dp"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="Campanario Blanco\nMistral Ice\nMistral\nMalibu" />

use this layout and let me know. 使用此布局并告诉我。

Thanks to @swanand-vaidya I tracked the error down. 感谢@ swanand-vaidya,我追踪了错误。 One of the parent's parent's had a wrap_content set. 父母的父母之一有一个wrap_content集。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/WET_ASPHALT"
android:orientation="vertical">

<TextView
    android:layout_width="270dp"
    android:layout_height="wrap_content" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#0D97FC"
    android:orientation="horizontal">

    <TextView

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:background="@color/AMETHYST_DARK"
        android:text="line1 hello world hello world hello world hello world \nline2 hello world hello\nline 3hello world\nline4 hello world world"
        android:textSize="14sp" />

</LinearLayout>

The result of this layout is 这种布局的结果是

在此输入图像描述

Line4 is not displayed. 不显示Line4。 As the top-most linear-layout has wrap_conent. 由于最顶部的线性布局具有wrap_conent。

When the top-most layout has a fixed value or match_parent. 当最顶层布局具有固定值或match_parent时。 All lines are correctly displayed. 所有行都正确显示。

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

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