簡體   English   中英

Android:TextView的奇怪文本換行問題

[英]Android: strange text wrapping issue with TextView

我在LinearLayout中有一個TextView。 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
在右側,我們看到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'不是。

錯誤2

如圖所示,總行數等於\\ n的總和,但它沒有考慮第一個字符串的包裝。 如果我們刪除長文本,它會按預期工作。 “Mistral Ice \\ nMistral \\ nMalibu”

沒有錯誤

你可以有這樣的東西,像這樣使用

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

使用此布局並告訴我。

感謝@ 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>

這種布局的結果是

在此輸入圖像描述

不顯示Line4。 由於最頂部的線性布局具有wrap_conent。

當最頂層布局具有固定值或match_parent時。 所有行都正確顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM