Android TextView文本未换行


76

谁能告诉我文本出了什么问题?长于一行的文本不会自动换行到下一行,而是超出屏幕范围。

以下是代码:

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

12
注意:您只需xmlns:android="http://schemas.android.com/apk/res/android"在根元素上使用。
杰里米·洛根

就我而言,我有一个很大的TextView显示一个数字,而在该数字的上方和下方都留有相当大的空间。我很快意识到,空间是其他字符。TextView似乎为诸如“ 2g”之类的数字下方的字符保留了该空间,其中“ g”的下部将使用该下部空间。
本杰明·巴斯马奇

Answers:


82

我自己修好了,关键是 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> 


33
更好地使用android:layout_weight=1而不是零宽度,这意味着textview将填充剩余的内容
shaman.sir 2010年

1
android:layout_weight =“ 1”在我的S3上不起作用,但是android:width =“ 0dip”可以吗?真奇怪 有什么解释吗?
productioncoder

如果使用GridLayout,则需要使用columnWeight,并且仅适用于v21或更高版本。
约翰尼·吴

如果我想将文本首尾设置为单行,则效果很好。如果我想将文本首尾设置为多于1行怎么办。在上述解决方案中,如果我设置android:singleLine =“ false”,则解决方案无效
tejraj

39

这个问题的唯一正确答案是,您需要将父级设置为适当的宽度(在本例FILL_PARENT中为WRAP_CONTENT),并android:layout_weight=1用于需要包装的textview。

SingleLine 默认情况下处于启用状态,因此不会进行任何更改。

width组为0px将工作,但不是一个很好的解决方案。

一些使用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));

19

您必须使用2个参数:

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

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


1
我发现我必须将android:maxLines =“ X”与这两个一起添加。X是任何数字(显然大于1),然后在Android 2.3上
换行了

13

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

android:singleLine="false".

希望它能工作。

祝一切顺利!


10

使用时,我无法获得所有这些解决方案TableLayout>TableRow>TextView。然后我发现了TableLayout.shrinkColumns="*"。唯一有效的其他解决方案是将TextView强制为layout_width:250pxetc,但我不喜欢强制这样的宽度。

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

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

注意你需要 shrinkColumns="*"

这显然在内<LinearLayout>。所以像<LinearLayout> <TableLayout> <TableRow> <TextView>

参考资料:

表格布局

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

希望能对某人有所帮助。


4

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

android:layout_width="wrap_content"

改成

android:layout_width="fill_parent" 

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


8
我认为fill_parent已弃用并替换为match_parent
RCB 2014年

3

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


3

对于我来说,删除输入类型确实可以解决问题,我使用的是android:inputType =“ textPostalAddress”,因为我的textview粘在一行上并且没有自动换行,因此解决了这个问题。


1

我是Android(和GUI)的初学者,但是有很多软件方面的经验。我已经阅读了一些教程,这是我的理解:

layout_width和layout_height是TextView的属性。它们是有关TextView应如何成形的说明,而不是指如何处理TextView中的内容。

如果使用“ fill_parent”,则是说TextView应该相对于其父视图调整自身形状,然后填充它。

如果使用“ wrap_content”,则表示您应该忽略父视图,并让TextView的内容定义其形状。

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

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


1

尽管这是一个老话题,但我想分享一下我的经验,因为它对我有帮助。我的应用程序在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"/>

希望这会帮助某人。



1

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


0

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


0

我知道,这个问题是正确的,但是就我而言,问题是在'dp'中设置了textSize属性-我将其更改为'sp'并且可以正常工作。


0

在我的情况下,用TableRow > ScrollView > TextView筑巢,我通过设置解决了这个问题android:layout_widthfill_parentTableRow,并wrap_contentScrollViewTextView


0

您必须android:singleLine="false"在urTextView标签中使用。


0

我终于设法在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>

希望能帮助到你。


0

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

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


0

我花了几个小时才弄清楚,我试图显示的文本中包含一个单引号(在string.xml中),所以我只是用反斜杠转义了它,并且效果很好=> TextView的高度正确地包裹了文本:

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

0

我主要使用约束布局。

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"

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


-1

我把这个属性:

android:inputType="textMultiLine"

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

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

当您来到这篇文章时,您可能想要创建一个可以显示多行的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.

7
这是用于EditText,而不是TextView。
贾斯汀

-1

我曾经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"/>

-1

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


并非在所有情况下都如此。考虑文本可能是动态的情况。
the_new_mr

-5

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


3
那就是只在给定的点打破界限。对于基于可用屏幕尺寸(视图宽度)的换行,我们需要其他一些东西。
hcpl
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.