[英]Android horizontal LinearLayout with wrapped text in TextView
[英]Android TextView Text not getting wrapped
谁能告诉我文字出了什么问题? 超过一行的文本不会换行到下一行,而是超出屏幕。
以下是代码:
<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>
我自己修好了,关键是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>
这个问题的唯一正确答案是你需要将父项设置为适当的宽度(在本例中为FILL_PARENT
, WRAP_CONTENT
),并使用android:layout_weight=1
作为需要包装的textview。
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));
您必须使用2个参数:
android:ellipsize="none"
:文本android:ellipsize="none"
宽度上没有剪切文本
android:scrollHorizontally="false"
文本根据需要包含多行
它足以在您的xml文件中使用。
android:singleLine="false".
希望它会奏效。
祝一切顺利!
使用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
希望能帮助别人。
您的代码中有一个布局参数错误。 在第一个TextView中
android:layout_width="wrap_content"
改成
android:layout_width="fill_parent"
超出屏幕宽度大小的文本将换行到下一行并设置android:singleline="false"
。
对于我的情况删除输入类型做了伎俩,我使用android:inputType =“textPostalAddress”,因为我的textview坚持一行而不是包装,删除这解决了问题。
设置文本视图的高度android:minHeight="some pixes"
或android:width="some pixels"
。 它将解决问题。
我是一名Android(和GUI)初学者,但拥有丰富的软件经验。 我已经完成了一些教程,这是我的理解:
layout_width和layout_height是TextView的属性。 它们是TextView应如何塑造自身的说明,它们并不是指如何处理TextView中的内容。
如果你使用“fill_parent”,你说TextView应该相对于它的父视图塑造自己,它应该填充它。
如果使用“wrap_content”,则表示应忽略父视图,并让TextView的内容定义它的形状。
我认为这是令人困惑的一点。 “wrap_content”并没有告诉TextView如何管理它的内容(包装行),它告诉它它应该相对于它的内容塑造自己。 在这种情况下,没有新的行字符,它会自行调整形状,以便所有文本都在一行上(不幸的是,它会溢出父节点)。
我想你希望它水平填充父级,并垂直包装它的内容。
即使这是一个老线程,我想分享我的经验,因为它帮助了我。 我的应用程序适用于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"/>
希望这会对某人有所帮助。
我把这个属性:
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.
增加高度即。 android:height =“有些大小”,它对我来说很好。
我有一个类似的问题,我的两个水平加权TextViews没有包装文本。 我后来发现问题是因为我的viewparents父亲有wrap_content而不是match_parent。
你必须在你的TextView
标签中使用android:singleLine="false"
。
我知道,这是正确的,但在我的情况下,问题是在'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
fill_parent
android:layout_width
设置为fill_parent
,并在ScrollView
和TextView
上设置wrap_content
来解决问题。
我认为这取决于显示器中特定的布局组合。 某些标志可能会被覆盖或忽略。 我有一个带标签的TabHost,每个标签都是一个表格列表。 所以它是ListView的一个标签,每一行都是TextView的TableLayout。 我尝试了上面列出的修复,但没有一个工作。
我终于设法在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>
希望能帮助到你。
我刚刚删除了android:lines="1"
并添加了android:maxLines="2"
,这使文本自动换行。 问题是android:lines
属性。 这导致文本包装不会发生。
我没有必要使用maxEms
或singleLine="false"
(不推荐使用的API)来解决这个问题。
我花了好几个小时才发现在我试图显示的文本中包含一个引号(在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) android:padding="12dp" android:layout_width="0dp" android:layout_weight="1" android:singleLine="false"
=文本将折叠以适应附近的布局,而不考虑文本本身
就我而言,我的文本视图有一个背景,因此将宽度应用为 0dp 对我不起作用,因为即使对于小文本,背景也会占用整个可用空间。 设法通过添加来解决它
app:layout_constrainedWidth="true"
无需将宽度设置为 0dp,包装内容工作正常,希望这可以帮助遇到同样问题的人
在你的 TextView 里面写这个:
android:inputType="textMultiLine"
问题解决了
我在我的字符串中间添加了\\ n,它看起来没问题。
要包装文本并将文本放在下一行,我们sholud使用布局xml文件中的“\\ n”即新行字符,并检查模拟器上的更改而不是布局屏幕上的更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.