【问题标题】:Android: strange text wrapping issue with TextViewAndroid:TextView 出现奇怪的文本换行问题
【发布时间】:2016-06-22 03:30:33
【问题描述】:

我在 LinearLayout 中有一个 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"
    />


在右侧,我们看到设置了文本的 textview:"Campanario Blanco\nMistral Ice\nMistral" 最后一个词没有显示。

在左边我们有另一个重量为 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);

现在如果我将视图的内容更改为 "Campanario Blanco\nMistral Ice\nMistral\nMalibu" 现在 'mistral' 是可见的,但新的最后一个作品 'Mailbu' 不可见。

如图所示,总行数等于 \n 的总和,但它没有考虑到第一个字符串的换行。 如果我们删除长文本,它会按预期工作。 “Mistral Ice\nMistral\nMalibu”

【问题讨论】:

  • 你在哪里设置行数?
  • 我没有行数。我将高度设置为 wrap_text。关于行数的信息,只是对它是如何自动换行的推断。
  • 奇怪。作为解决方法,您可以添加一个额外的换行符。 TextView 的错误?
  • 是的,但可以说如果我设置“非常长的字符串 A\n 非常长的字符串 B\n 短字符串”我将不得不添加 2 行额外的行 - 这可以工作 - 但由于多个屏幕尺寸/resolutions 我不知道“非常长的字符串 A”是否使用一两行。

标签: android android-layout user-interface textview word-wrap


【解决方案1】:

你可以有这样的东西,像这样使用

<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" />

使用此布局并告诉我。

【讨论】:

  • 谢谢。我最初的布局是这样的,它应该可以工作,但它不是。所以我在层次结构中查找,其中一个父线性布局设置为宽度:WRAP_CONTENT,其中一个子布局具有固定宽度。所以发生了这个奇怪的包装错误。我更新了我的答案。
【解决方案2】:

感谢@swanand-vaidya,我找到了错误。其中一个父母的父母有一个 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>

这个布局的结果是

不显示第 4 行。 因为最顶层的线性布局有 wrap_conent。

当最顶层布局具有固定值或 match_parent 时。 所有行都正确显示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2013-09-24
    • 2012-01-30
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多