Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.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中的多行文本视图?_Android_Textview_Tablelayout_Multiline - Fatal编程技术网

Android中的多行文本视图?

Android中的多行文本视图?,android,textview,tablelayout,multiline,Android,Textview,Tablelayout,Multiline,我确实喜欢下面的xml <TableRow> <TextView android:id="@+id/address1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:maxLines="4" android:singleLine=

我确实喜欢下面的
xml

<TableRow>
    <TextView android:id="@+id/address1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="left"
        android:maxLines="4" 
        android:singleLine="false"              
        android:text="Johar Mor, Gulistan-e-Johar, Karachi" >
    </TextView> 
</TableRow>

它不适用于
多行
,我正在使用
表格布局


那么我在这里犯了什么错误呢?

TextView将是多行的,因为它没有足够的空间容纳单行,并且singLine没有设置为true


如果在一行中获得空格,则不会是多行。

如果在
文本视图中输入的文本较短,则不会自动扩展到四行。如果希望
TextView
始终具有四行,而不管其中的文本长度如何,请设置
android:lines
属性:

<TextView
    android:id="@+id/address1"
    android:gravity="left"
    android:layout_height="fill_parent"
    android:layout_width="wrap_content"
    android:maxLines="4"
    android:lines="4"
    android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."></TextView>

您可以使用TableRow执行此操作,请参见下面的代码

<TableRow >
        <TextView
            android:id="@+id/tv_description_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:padding="8dp"
            android:text="@string/rating_review"
            android:textColor="@color/black"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:maxLines="4"`enter code here`
            android:padding="8dp"
            android:text="The food test was very good."
            android:textColor="@color/black"
            android:textColorHint="@color/hint_text_color" />
    </TableRow>


我不喜欢强制文本视图中的行数的解决方案。我建议你通过解决方案来解决它。正如我所见,OP也在努力使文本视图在表中看起来合适,而
shrinkColumns
是实现所需的正确指令。

我不喜欢这两个答案。只需设置inputType,TextView就会适应其内容

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"/>


在NexusOne(2.3)和Nexus4(4.4)上进行测试。

只需在ScrollView中添加textview即可

<ScrollView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:layout_marginLeft="15dp"
           android:layout_marginRight="15dp"
           android:layout_marginTop="20dp"
           android:fillViewport="true">

           <TextView
               android:id="@+id/txtquestion"
               android:layout_width="fill_parent"
               android:layout_height="match_parent"
               android:background="@drawable/abs__dialog_full_holo_light"
               android:lines="20"
               android:scrollHorizontally="false"
               android:scrollbars="vertical"
               android:textSize="15sp" />

       </ScrollView>

我用过:

TableLayout tablelayout = (TableLayout) view.findViewById(R.id.table);
tablelayout.setColumnShrinkable(1,true);

这对我有用。1是列数。

我想如果您使用:

android:inputType=“textMultiLine”

然后视图不再可单击。我试图在我的幻灯片抽屉菜单中获得多行文本视图,这显然需要对点击做出响应


不过,
android:singleLine=“false”
运行良好。

只需在文本中添加“\n”。。。不需要额外的属性:)


为什么不声明一个字符串,而不是在布局文件中写入一长行呢。为此,您必须在布局文件“android:text=“@string/text”中声明一行代码,然后转到\\app\src\main\res\values\strings.xml并添加一行代码“Your multiline text here”也使用“\n”从任何点断开该行,否则该行将自动调整。

我学到的是添加“\n”在你想让它刹车进入下一行的字里行间。例如

<TextView
    android:id="@+id/time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:text="M-F 9am-5pm \n By Appointment Only" />


下午5点到下午4点之间的时间允许我更好地控制下一行的开始和结束位置。

尝试使用EditText,使其不可浏览和聚焦,还可以显示滚动条并删除EditText的底部。
以下是一个例子:

<EditText
android:id="@+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_parent"
android:inputType="textMultiLine"                   <!-- multiline -->
android:clickable="false"                         <!-- unclickable -->
android:focusable="false"                         <!-- unfocusable -->
android:scrollbars="vertical"     <!-- enable scrolling vertically -->
android:background="@android:color/transparent"   <!-- hide the underbar of EditText -->
/>  

android:focusable=“false”
android:scrollbars=“垂直”
android:background=“@android:color/transparent”
/>  

希望这有帮助:)

我找到的多行文本视图的最佳答案是:

android:inputType="textMultiLine"
首先将
“\n”
替换为其
Html
等价物
“br”
然后对字符串调用
Html.fromHtml()
。请遵循以下步骤:

String text= model.getMessageBody().toString().replace("\n", "&lt;br&gt;")
textView.setText(Html.fromHtml(Html.fromHtml(text).toString()))

这非常有效。

对我来说,没有一种解决方案不起作用。 我知道这不是这个问题的完整答案,但有时它会有所帮助

我将4个文本视图放在一个垂直线性布局中

<Linearlayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation:"vertical"

    ...>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text:"line1"
       .../>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text:"line2"
       .../>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text:"line3"
       .../>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text:"line4"
       .../>
</LinearLayout>

此解决方案适用于文本视图文本为常量的情况,如标签。

您可以使用TableRow执行此操作,请参见下面的代码
You can do this with TableRow, see below code

<TableRow >
    <TextView
        android:id="@+id/tv_description_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:padding="8dp"
        android:text="@string/rating_review"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:maxLines="4"`enter code here`
        android:padding="8dp"
        android:text="The food test was very good."
        android:textColor="@color/black"
        android:textColorHint="@color/hint_text_color" />
</TableRow>

您可以使用android:inputType=“textMultiLine”


最简单的方法

<TableRow>
    <TextView android:id="@+id/address1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="left"
        android:maxLines="4" 
        android:singleLine="false"              
        android:text="Johar Mor,\n Gulistan-e-Johar,\n Karachi" >
    </TextView> 
</TableRow>

在要插入新行的位置使用
\n

希望它能帮助您

关键是一个文本视图,其中包含
singleline=false
(不推荐使用该选项,但必须使用该选项)以及
最大行
最小行

<TextView
                android:id="@+id/txtBowlers"
                style="@style/Default_TextBox.Small"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:singleLine="false"
                android:maxLines="6"
                android:text="Bob\nSally\nJohn"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="@+id/txtTeamName"
                app:layout_constraintTop_toBottomOf="@+id/txtTeamName"/>

我希望这些答案有点陈旧,只需更改资源布局文件中的输入类型即可解决您的问题。例如:

<EditText
        android:id="@+id/notesInput"
        android:hint="Notes"
        android:inputType="textMultiLine"
/>

以下代码可用于单行和多行文本视图

isMultiLine=如果为真,则文本视图显示多行,否则为单行

    if (isMultiLine) {
        textView.setElegantTextHeight(true);
        textView.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        textView.setSingleLine(false);
        
    } else {
        textView.setSingleLine(true);
        textView.setEllipsize(TextUtils.TruncateAt.END);
    }

如果我想使用填充父对象而不是固定宽度,该怎么办?我的内容被剪切了,如果我这样做的话……我会尝试
android:singleLine=“false”
android:inputType=“textMultiLine”
(如Marageus所述)。请注意,
android:inputType=“textMultiLine”
在共享其他发表评论的人的观点时优先,这是完全错误的,我对为什么这是公认的答案感到目瞪口呆。android:inputType=“textMultiLine”不适用于TextView,它适用于EditText,如果您尝试这样做,您会收到警告,您应该听警告。使用@Ryans解决方案。OP不想“不管文本的长度如何,文本视图总是有四行”。这个答案对我很有用。但是我得到了最好的结果,不是用maxlines和lines,而是用maxlines和minlines,而不是用lines。我将minlines设置为1,maxlines大于最大数据。这样我就可以得到1到N行数据的压缩视图。@REarleHarris:我也尝试过同样的解决方案,但问题是它没有计算最大行数。所有数据均以最小行数显示。我现在在XML中使用自定义文本视图。此解决方案不能用于设置文本dynamically@SagarDevanga动态设置文本意味着什么?@SagarDevanga,我刚刚检查过,动态和静态文本默认是多行的(如果使用
\n
),所以这是一个正确的解决方案。没有
singleLine=false
,没有
lines=4
等等。这并不完全正确,因为Android自身的lint错误表明您只对EditText使用此属性,而不对TextView使用。您是指textMultiline吗?我说过不要用那个。singleLine在TextViews上很好:android:singleLine现在不推荐使用,你确定吗?是的
<EditText
        android:id="@+id/notesInput"
        android:hint="Notes"
        android:inputType="textMultiLine"
/>
    if (isMultiLine) {
        textView.setElegantTextHeight(true);
        textView.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        textView.setSingleLine(false);
        
    } else {
        textView.setSingleLine(true);
        textView.setEllipsize(TextUtils.TruncateAt.END);
    }