【问题标题】:Android TextView Text not getting wrappedAndroid TextView 文本没有被包装
【发布时间】:2023-03-06 11:57:01
【问题描述】:

谁能告诉我文字出了什么问题?超过一行的文本不会换行到下一行,而是超出屏幕。

以下是代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:padding="4dip">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:padding="4dip">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            android:padding="4dip">
            <TextView 
                android:id="@+id/reviewItemEntityName"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="event/venue" 
                android:textColor="@color/maroon"
                android:singleLine="true" 
                android:ellipsize="end" 
                android:textSize="14sp"
                android:textStyle="bold" 
                android:layout_weight="1" />

            <ImageView 
                android:id="@+id/reviewItemStarRating"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignParentBottom="true"
                android:src="@drawable/title_1_star" />
        </LinearLayout>

        <TextView 
            android:id="@+id/reviewItemDescription"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:text="Description comes here" 
            android:textSize="12sp" 
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 注意:你只需要在根元素上有xmlns:android="http://schemas.android.com/apk/res/android"
  • 在我的例子中,我有一个非常大的 TextView 显示一个数字,并且在该数字的上方和下方都有相当大的空间。我很快意识到,这个空间的用途是其他角色。 TextView 似乎为“2g”之类的数字以下的字符保留了该空间,其中“g”的下部将使用该较低空间。

标签: android textview


【解决方案1】:

我自己修好了,关键是android:width="0dip"

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="4dip"
    android:layout_weight="1">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="4dip">

        <TextView
            android:id="@+id/reviewItemEntityName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/maroon"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="14sp"
            android:textStyle="bold"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/reviewItemStarRating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true" />
        </LinearLayout>

        <TextView
            android:id="@+id/reviewItemDescription"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:width="0dip" />

    </LinearLayout>

    <ImageView
        android:id="@+id/widget01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_nxt"
        android:layout_gravity="center_vertical"
        android:paddingRight="5dip" />

</LinearLayout> 

【讨论】:

  • 最好使用android:layout_weight=1而不是零宽度,这意味着textview将填充剩余的内容
  • android:layout_weight="1" 在我的 S3 上不起作用,但 android:width="0dip" 可以吗?这很奇怪。有什么解释吗?
  • 如果您使用 GridLayout,您将需要使用 columnWeight,它仅适用于 v21 或更新版本。
  • 如果我想在单行中将文本首尾相连,这非常有用。如果我想将文本端到端设置超过 1 行怎么办。在上述解决方案中,如果我设置 android:singleLine="false", 则解决方案不起作用
【解决方案2】:

这个问题唯一正确的答案是你需要将父母设置为适当的宽度(在这种情况下FILL_PARENTWRAP_CONTENT)并使用android:layout_weight=1作为需要包装的文本视图。

SingleLine 默认开启,因此不会进行任何更改。

width 设置为 0px 可以工作,但不是一个好的解决方案。

一些例子(这次是在tableview中),使用xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <TableLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:stretchColumns="*"
        android:id="@+id/tableLayout1">
        <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView android:text="test1" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="0" />
            <TextView android:layout_weight="1"
                android:text="test2 very long text that needs to be wrapped properly using layout_weight property and ignoring singleline since that is set by default..."
                android:layout_width="fill_parent" android:layout_height="wrap_content" />
        </TableRow>     
    </TableLayout>
</LinearLayout>

如果你想在代码中设置它,你正在寻找 layout_weight 作为第三个参数,如本例中设置为 1:

row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
TextView label = new TextView(getApplicationContext());
label.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT, 1f));

【讨论】:

    【解决方案3】:

    您必须使用 2 个参数:

    • android:ellipsize="none" : 文本未在 textview 宽度上剪切

    • android:scrollHorizontally="false" 文本根据需要换行

    【讨论】:

    • 我发现在我的情况下,我必须将 android:maxLines="X" 与这两个一起添加。 X 是任意数字(显然大于 1)然后在 Android 2.3 上包装的文本
    【解决方案4】:

    在你的xml文件中使用就足够了。

    android:singleLine="false".
    

    希望它会起作用。

    一切顺利!

    【讨论】:

      【解决方案5】:

      使用TableLayout&gt;TableRow&gt;TextView 时,我无法使这些解决方案中的任何一个工作。然后我找到了TableLayout.shrinkColumns="*"。唯一可行的其他解决方案是将 TextView 强制为 layout_width:250px 等,但我不喜欢这样强制宽度。

      如果使用表格,请尝试这样的操作。

                  <TableLayout 
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:shrinkColumns="*">
      

      注意你需要shrinkColumns="*"

      这显然在&lt;LinearLayout&gt; 内。所以像&lt;LinearLayout&gt; &lt;TableLayout&gt; &lt;TableRow&gt; &lt;TextView&gt;

      参考:

      TableLayout

      http://code.google.com/p/android/issues/detail?id=4000

      希望对某人有所帮助。

      【讨论】:

        【解决方案6】:

        您的代码中有一个布局参数有误。在第一个 TextView

        android:layout_width="wrap_content"
        

        改成

        android:layout_width="fill_parent" 
        

        超出屏幕宽度大小的文本将换行并设置android:singleline="false"

        【讨论】:

        • 我认为 fill_parent 已被弃用并替换为 match_parent
        【解决方案7】:

        设置文本视图的高度android:minHeight="some pixes"android:width="some pixels"。它会解决问题。

        【讨论】:

          【解决方案8】:

          在我的情况下,删除输入类型起到了作用,我使用的是 android:inputType="textPostalAddress",因为我的 textview 被粘在一行上并且没有换行,删除这个解决了这个问题。

          【讨论】:

            【解决方案9】:

            我是一名 Android(和 GUI)初学者,但拥有丰富的软件经验。我已经阅读了一些教程,这是我的理解:

            layout_width 和 layout_height 是 TextView 的属性。它们是关于 TextView 应该如何塑造自身的说明,它们并不是指如何处理 TextView 中的内容。

            如果你使用“fill_parent”,你是说 TextView 应该相对于它的父视图来塑造自己,它应该填充它。

            如果你使用“wrap_content”,你的意思是你应该忽略父视图,让 TextView 的内容定义它的形状。

            我认为这是令人困惑的地方。 “wrap_content”并没有告诉 TextView 如何管理它的内容(以换行),它告诉它应该相对于它的内容来塑造自己。在这种情况下,没有换行符,它会自行调整,以便所有文本都在一行上(不幸的是,它溢出了父行)。

            我认为您希望它水平填充父级,并垂直包装它的内容。

            【讨论】:

              【解决方案10】:

              尽管这是一个旧线程,但我想分享我的经验,因为它对我有帮助。我的应用程序在 OS 2.0 和 4.0+ 上运行良好,但对于运行 OS 3.x 的 HTC 手机,文本没有换行。对我有用的是包含这两个标签。

              android:maxLines="100"
              android:scrollHorizontally="false"
              

              如果您消除其中任何一个,则它仅适用于 os 3.0 设备。 “ellipsize”参数具有中性效果。这是下面的完整textview标签

              <TextView
                              android:id="@+id/cell_description"
                              android:layout_width="fill_parent"
                              android:layout_height="wrap_content"
                              android:paddingTop="5dp"
                              android:maxLines="100"
                              android:scrollHorizontally="false"
                              android:textStyle="normal"
                              android:textSize="11sp"
                              android:textColor="@color/listcell_detail"/>
              

              希望这会对某人有所帮助。

              【讨论】:

                【解决方案11】:

                增加高度,即 android:height="Some size" ,对我来说效果很好。

                【讨论】:

                  【解决方案12】:

                  我有一个类似的问题,我的两个水平加权 TextView 没有换行文本。后来我发现这个问题是因为我的 viewparents 父母有 wrap_content 而不是 match_parent。

                  【讨论】:

                    【解决方案13】:

                    我认为这取决于您的显示器中的特定布局组合。某些标志可能会被覆盖或忽略。我有一个带有选项卡的 TabHost,每个选项卡都是一个表列表。所以它是ListView的一个tab,每一行都是TextView的一个TableLayout。我尝试了上面列出的修复,但都没有奏效。

                    【讨论】:

                      【解决方案14】:

                      我知道,有问题的是它是正确的,但就我而言,问题在于在“dp”中设置 textSize 属性 - 我已将其更改为“sp”,它工作正常。

                      【讨论】:

                        【解决方案15】:

                        在我的例子中,使用 TableRow &gt; ScrollView &gt; TextView 嵌套,我通过在 TableRow 上将 android:layout_width 设置为 fill_parent 以及在 ScrollViewTextView 上设置 wrap_content 来解决问题。

                        【讨论】:

                          【解决方案16】:

                          你必须在你的TextView标签中使用android:singleLine="false"

                          【讨论】:

                            【解决方案17】:

                            我终于设法在 TextView 的高度上添加了一些像素来解决这个问题。

                            首先,您需要实际获取 TextView 的高度。这并不简单,因为它在绘制之前为 0。

                            将此代码添加到 onCreate:

                            mReceiveInfoTextView = (TextView) findViewById(R.id.receive_info_txt);
                            if (mReceiveInfoTextView != null) { 
                                final ViewTreeObserver observer = mReceiveInfoTextView.getViewTreeObserver();
                                observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                                    @Override
                                    public void onGlobalLayout() {
                                        int height = mReceiveInfoTextView.getHeight();
                                        int addHeight = getResources().getDimensionPixelSize(R.dimen.view_add_height);
                                        mReceiveInfoTextView.setHeight(height + addHeight);
                            
                                        // Remove the listener if possible
                                        ViewTreeObserver viewTreeObserver = mReceiveInfoTextView.getViewTreeObserver();
                                        if (viewTreeObserver.isAlive()) {
                                            viewTreeObserver.removeOnGlobalLayoutListener(this);
                                        }
                                    }
                                });
                            }
                            

                            您需要将此行添加到 dimens.xml

                            <dimen name="view_add_height">10dp</dimen>
                            

                            希望对你有帮助。

                            【讨论】:

                              【解决方案18】:

                              我刚刚删除了android:lines="1" 并添加了android:maxLines="2",这使文本自动换行。问题是android:lines 属性。这会导致文本换行发生。

                              我不必使用 maxEmssingleLine="false"(已弃用的 API)来解决此问题。

                              【讨论】:

                                【解决方案19】:

                                我花了好几个小时才发现在我试图显示的文本中包含一个单引号(在 string.xml 中)所以我只是用反斜杠转义它,它工作得很好 => height 正确地换行了文本:

                                <?xml version="1.0" encoding="utf-8"?>
                                <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:orientation="vertical"
                                    android:paddingLeft="16dp"
                                    android:paddingRight="16dp"
                                    android:paddingTop="16dp"
                                    tools:context=".MainActivity">
                                
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:fontFamily="sans-serif-smallcaps"
                                        android:text="@string/instructions"
                                        android:textAlignment="center"
                                        android:textSize="18sp"
                                        android:textStyle="bold" />
                                
                                    <EditText
                                        android:id="@+id/keyword"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:text="@string/defaultKeyword"
                                        android:textSize="22sp"
                                        />
                                

                                【讨论】:

                                  【解决方案20】:

                                  我主要使用约束布局。

                                  1) android:layout_width="match_parent" = 尝试拉伸以接触边缘

                                  2) android:layout_width="wrap_content" = 仅基于输入文本,不考虑附近的其他视图。例如添加android:textAlignment="center" 会改变文本的形状

                                  3)android:padding="12dp" android:layout_width="0dp" android:layout_weight="1" android:singleLine="false"

                                  = 文本将折叠以适应附近的布局,而不考虑文本本身

                                  【讨论】:

                                    【解决方案21】:

                                    在我的例子中,我的文本视图有一个背景,所以将宽度应用为 0dp 对我来说不起作用,因为即使是小文本,背景也会占据整个可用空间。设法通过添加来解决它

                                    app:layout_constrainedWidth="true"
                                    

                                    无需将宽度设置为 0dp,换行效果很好,希望对遇到同样问题的人有所帮助

                                    【讨论】:

                                      【解决方案22】:

                                      我把这个属性:

                                      android:inputType="textMultiLine"
                                      

                                      进入我的TextView,它有换行,用户可以“输入”换行。

                                      ================================================ ==============

                                      当你看到这篇文章时,你可能想要创建一个可以显示多行的 Big TextView,因此可能还需要这些属性

                                      android:layout_height="Xdp" //where X is a number depends on how big Textview you want
                                      android:gravity="top" //in order to make your text start from the top of your TextView.
                                      

                                      【讨论】:

                                      • 这是用于 EditText,而不是 TextView。
                                      【解决方案23】:

                                      我使用android:ems="23" 来解决我的问题。只需将 23 替换为您情况下的最佳值即可。

                                      <TextView
                                              android:id="@+id/msg"
                                              android:ems="23"
                                              android:text="ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab "
                                              android:textColor="@color/white"
                                              android:textStyle="bold"
                                              android:layout_width="wrap_content"
                                              android:layout_height="wrap_content"/>
                                      

                                      【讨论】:

                                        【解决方案24】:

                                        我在字符串中间添加了一个\n,看起来还不错。

                                        【讨论】:

                                        • 这不会在所有情况下。考虑文本可能是动态的情况。
                                        【解决方案25】:

                                        要换行并将文本放在下一行,我们应该使用“\n”,即布局 xml 文件中的换行符,并检查模拟器上的更改而不是布局屏幕上的更改。

                                        【讨论】:

                                        • 那是只在某个给定点断线。对于基于可用屏幕尺寸(视图宽度)的包装,我们需要其他东西。
                                        猜你喜欢
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 2012-02-02
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        相关资源
                                        最近更新 更多