Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android TextView文本未被包装_Android_Textview - Fatal编程技术网

Android TextView文本未被包装

Android TextView文本未被包装,android,textview,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

谁能告诉我课文出了什么问题吗?超过一行的文本不会换行到下一行,但会超出屏幕

代码如下:

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

我自己修好了,钥匙是安卓:width=“0dip”



您必须在ur
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=“一些像素”
android:width=“一些像素”
。它将解决问题。

当使用
TableLayout>TableRow>TextView
时,这些解决方案中的任何一个都无法工作。然后我找到了
TableLayout.shrinkColumns=“*”
。唯一有效的另一个解决方案是强制TextView使用
layout\u width:250px
等,但我不喜欢强制使用那样的宽度

如果使用表格,请尝试类似的操作

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

注意:您需要
shrinkColumns=“*”

这显然在
中。比如说

参考资料:


希望对某人有所帮助。

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

此问题的唯一正确答案是需要将父项设置为适当的宽度(在这种情况下,
FILL\u PARENT
WRAP\u CONTENT
)并对需要包装的文本视图使用
android:layout\u weight=1

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

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

使用xml的一些示例(这次在tableview中):

<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>
我是一名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"/>


希望这能对其他人有所帮助。

在xml文件中使用就足够了

android:singleLine="false".
希望它能起作用


最好的!

对于我的案例,删除输入类型成功了,我使用的是android:inputType=“textPostalAddress”,因为我的textview被粘贴到一行,没有包装,删除它修复了问题。

我将此属性:

android:inputType="textMultiLine"
进入我的文本视图,它有换行符,用户可以“输入”新行。

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

当你来到这篇文章,你可能想创建一个大的文本视图,它可以显示多行,所以这些属性可能也需要

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.

增加高度,即安卓:height=“Some size”,对我来说效果很好。

我知道,有问题的是它是正确的,但在我的例子中,问题是在“dp”中设置textSize属性-我已将其更改为“sp”,效果很好。

我使用了
安卓: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
嵌套,我通过将
android:layout\u width
设置为
TableRow
上的
fill\u parent
,以及
ScrollView
TextView

上的
wrap\u内容,解决了这个问题。我的两个水平视图在哪里出现了类似的问题加权文本视图没有包装文本。我后来发现问题是因为我的viewparents父视图具有包装内容,而不是匹配父视图。

我终于设法在文本视图的高度上添加了一些像素来解决此问题

首先,您需要实际获取TextView的高度。这并不简单,因为之前它是0
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.
<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"/>
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);
            }
        }
    });
}
<dimen name="view_add_height">10dp</dimen>
<?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"
        />