Android TextView 文本没有被包装

有人能告诉我短信出了什么问题吗?长于一行的文本不会换行到下一行,而是超出屏幕。

密码如下:

<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>
149194 次浏览

我自己修好的,钥匙是 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>

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

代码中的一个布局参数出错

android:layout_width="wrap_content"

变成

android:layout_width="fill_parent"

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

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

你必须使用两个参数:

  • android:ellipsize="none": 文本在文本视图宽度上不被剪切

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

设置文本视图 android:minHeight="some pixes"或者 android:width="some pixels"的高度,这样就可以解决问题了。

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

如果使用表格,可以尝试这样做。

            <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

希望这对某人有帮助。

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

这个问题的唯一正确答案是,您需要将父视图设置为适当的宽度(在本例中为 FILL_PARENTWRAP_CONTENT) ,并对需要包装的文本视图使用 android:layout_weight=1

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>

如果你想在代码中设置这个参数,你需要在第三个参数中寻找 layyweight,在这个例子中,它被设置为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));

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

Lay-width 和 lay-height 是 TextView 的属性。它们是关于 TextView 应该如何塑造自身的说明,而不是关于如何处理 TextView 中的内容。

如果使用“ fill _ father”,那么就是说 TextView 应该相对于它的父视图来塑造自己的形状,它应该填充它。

如果您使用“ wrash _ content”,那么您就是在说您应该忽略父视图,让 TextView 的内容定义它的形状。

我觉得这就是让人困惑的地方。“ wrash _ content”并没有告诉 TextView 如何管理它的内容(包装行) ,而是告诉 TextView 应该相对于它的内容来塑造自己的形状。在这种情况下,没有新的行字符,它将自己形状化,以便所有文本都在一行上(不幸的是,这将溢出父行)。

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

虽然这是一个老主题,我想分享我的经验,因为它帮助了我。我的应用程序在 OS 2.0和4.0 + 上运行良好,但是对于运行 OS 3.x 的 HTC 手机,文本没有包装。对我有效的方法是同时包含这两个标签。

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

如果你排除任何一个,它不工作只有 OS 3.0设备。“椭圆大小”参数具有中性效应。下面是完整的 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"/>

希望这能帮到别人。

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

android:singleLine="false".

希望能成功。

一切顺利!

对于删除输入类型的例子,我使用了 android: inputType = “ textPostalAddress”,因为我的文本视图粘贴在一行上,没有进行包装,删除它就解决了这个问题。

我把这个属性:

android:inputType="textMultiLine"

进入我的 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.

增加高度,例如。 android: height = “ Some size”,它对我来说工作得很好。

我知道,问题是它是正确的,但是在我的例子中,问题是在‘ dp’中设置 textSize 属性-我把它改为‘ sp’,它工作得很好。

我使用 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"/>

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

我有一个类似的问题,我的两个水平加权的 TextView 没有包装文本。后来我发现这个问题是因为我的视图父节点具有 wrp _ content 而不是 match _ father。

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

您需要将此行添加到 Xml

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

希望能有帮助。

我只是删除 android:lines="1"和添加 android:maxLines="2",这得到的文本自动换行。问题在于 android:lines属性。这会导致文本换行到 没有

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

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

我花了好几个小时才弄明白,我试图显示的文本中包含一个引号(在 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"
/>

我主要使用约束布局。

1) android:layout_width="match_parent" = 尝试拉伸以满足边缘

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

3) < code > android: 填充 = “12dp” 布局宽度 = “0dp” 布局 _ 重量 = “1” Android: singleLine = “ false”

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

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

app:layout_constrainedWidth="true"

不需要设置宽度为0dp,包装内容工作正常,希望这对有同样问题的人有所帮助

在你的 TextView 中写下:

android:inputType="textMultiLine"

问题解决了