Android TextView 文本没有被包裹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2197744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android TextView Text not getting wrapped
提问by d-man
Can anyone tell me what's going wrong with the text? Text longer than one line doesn't wrap to the next line but goes beyond the screen.
谁能告诉我文本出了什么问题?超过一行的文本不会换行到下一行,而是超出屏幕。
Following is the code:
以下是代码:
<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>
回答by d-man
I fixed it myself, the key is android:width="0dip"
我自己修的,关键是 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>
回答by hcpl
The only correct answer to this question is that you need to set the parents to a proper width (in this case FILL_PARENT
, WRAP_CONTENT
) and use android:layout_weight=1
for the textview that needs to be wrapped.
这个问题的唯一正确答案是您需要将父项设置为适当的宽度(在本例中为FILL_PARENT
, WRAP_CONTENT
)并android:layout_weight=1
用于需要包装的文本视图。
SingleLine
is on by default so that won't make any changes.
SingleLine
默认情况下处于启用状态,因此不会进行任何更改。
A width
set to 0px will work but is not a good solution.
一width
组为0px将工作,但不是一个很好的解决方案。
Some example (in a tableview this time), using xml:
一些示例(这次在 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>
If you want to set this in code you're looking for the layout_weight as a third parameter as in this example where it is set to 1:
如果你想在代码中设置它,你正在寻找 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));
回答by Kartaguez
You must use 2 parameters :
您必须使用 2 个参数:
android:ellipsize="none"
: the text is not cut on textview widthandroid:scrollHorizontally="false"
the text wraps on as many lines as necessary
android:ellipsize="none"
: 文本未在 textview 宽度上剪切android:scrollHorizontally="false"
文本根据需要在尽可能多的行上换行
回答by Dhasneem
It is enough to use in your xml file.
在您的 xml 文件中使用就足够了。
android:singleLine="false".
Hope it will work.
希望它会起作用。
All the best!
祝一切顺利!
回答by wired00
I could not get any of these solutions working when using a TableLayout>TableRow>TextView
. I then found TableLayout.shrinkColumns="*"
. The only other solution that worked was forcing the TextView to layout_width:250px
etc but i don't like forcing widths like that.
使用TableLayout>TableRow>TextView
. 然后我发现TableLayout.shrinkColumns="*"
. 唯一有效的其他解决方案是强制 TextViewlayout_width:250px
等,但我不喜欢强制那样的宽度。
Try something like this if working with tables.
如果使用表格,请尝试这样的操作。
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*">
Note you need shrinkColumns="*"
注意你需要 shrinkColumns="*"
This is obviously within a <LinearLayout>
. So something like <LinearLayout> <TableLayout> <TableRow> <TextView>
这显然在一个<LinearLayout>
. 所以像<LinearLayout> <TableLayout> <TableRow> <TextView>
references:
参考:
http://code.google.com/p/android/issues/detail?id=4000
http://code.google.com/p/android/issues/detail?id=4000
Hope that helps someone.
希望能帮助某人。
回答by wired00
One of your layout parameters is wrong in your code. In the first TextView
您的代码中的布局参数之一是错误的。在第一个 TextView
android:layout_width="wrap_content"
change to
改成
android:layout_width="fill_parent"
The text that out of screen width size will wrap to next line and set android:singleline="false"
.
超出屏幕宽度大小的文本将换行到下一行并设置android:singleline="false"
.
回答by Chandra
Set the height of the text view android:minHeight="some pixes"
or android:width="some pixels"
. It will solve the problem.
设置文本视图的高度android:minHeight="some pixes"
或android:width="some pixels"
. 它将解决问题。
回答by iBabur
For my case removing input type did the trick, i was using android:inputType="textPostalAddress" due to that my textview was sticked to one line and was not wrapping, removing this fixed the issue.
对于我的情况,删除输入类型可以解决问题,我使用 android:inputType="textPostalAddress" 因为我的 textview 被粘在一行上并且没有换行,删除这个解决了这个问题。
回答by Ben Horner
I'm an Android (and GUI) beginner, but have lots of experience with software. I've gone through a few of the tutorials, and this is my understanding:
我是 Android(和 GUI)初学者,但对软件有很多经验。我已经阅读了一些教程,这是我的理解:
layout_width and layout_height are attributes of the TextView. They are instructions for how the TextView should shape itself, they aren't referring to how to handle the content within the TextView.
layout_width 和 layout_height 是 TextView 的属性。它们是关于 TextView 应该如何塑造自身的说明,它们不是指如何处理 TextView 中的内容。
If you use "fill_parent", you are saying that the TextView should shape itself relative to it's parent view, it should fill it.
如果你使用“fill_parent”,你是说 TextView 应该相对于它的父视图来塑造自己,它应该填充它。
If you use "wrap_content", you are saying that you should ignore the parent view, and let the contents of the TextView define it's shape.
如果你使用“wrap_content”,你是说你应该忽略父视图,让 TextView 的内容定义它的形状。
I think this is the confusing point. "wrap_content" isn't telling the TextView how to manage it's contents (to wrap the lines), it's telling it that it should shape itself relative to it's contents. In this case, with no new line characters, it shapes itself so that all the text is on a single line (which unfortunately is overflowing the parent).
我认为这是令人困惑的一点。“wrap_content”并没有告诉 TextView 如何管理它的内容(包装线条),它告诉它应该相对于它的内容来塑造自己。在这种情况下,没有换行符,它会调整自己的形状,以便所有文本都在一行上(不幸的是,它溢出了父行)。
I think you want it to fill the parent horizontally, and to wrap it's contents vertically.
我认为您希望它水平填充父级,并垂直包装其内容。
回答by nsuinteger
Even-though this is an old thread, i'd like to share my experience as it helped me. My application was working fine for OS 2.0 & 4.0+ but for a HTC phone running OS 3.x the text was not wrapping. What worked for me was to include both of these tags.
尽管这是一个旧线程,但我想分享我的经验,因为它对我有帮助。我的应用程序在 OS 2.0 和 4.0+ 上运行良好,但对于运行 OS 3.x 的 HTC 手机,文本没有换行。对我有用的是包含这两个标签。
android:maxLines="100"
android:scrollHorizontally="false"
If you eliminate either it was not working for only the os 3.0 device. "ellipsize" parameter had neutral effect. Here is the full textview tag below
如果您消除其中任何一个,则它仅适用于 os 3.0 设备。“ellipsize”参数具有中性效果。这是下面的全文视图标签
<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"/>
Hope this would help someone.
希望这会帮助某人。