Android:动态添加的 TextView 不换行文本

标签 android xml textview

将 TextView 动态添加到此布局会导致文本不换行。

<!-- R.layout.description_card -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout_InfoShow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/table_row_padding_bottom" >

    <TableLayout 
        android:background="@drawable/card_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:id="@+id/TableRow_DescriptionTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/DescriptionTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="sans-serif-light"
                    android:textSize="@dimen/page_name_size"
                    android:text="Loading data..." />

        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/DescriptionTitle">

                <LinearLayout android:id="@+id/LinearLayout_Description"
                    android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                        <!-- Dynamically added TextView here -->

                </LinearLayout>

        </TableRow>
    </TableLayout>

</LinearLayout>

我使用这些方法将 TextView 动态添加到此布局:

    View view = inflater.inflate(R.layout.description_card, null);
                        TextView title = (TextView) view.findViewById(R.id.DescriptionTitle);
                        LinearLayout description = (LinearLayout) view.findViewById(R.id.LinearLayout_Description);
ArrayList<TextView> textViews = des.getText();
            Iterator<TextView> iter = textViews.iterator();
                 while(iter.hasNext()){
                    TextView textView = iter.next();
                    description.addView(textView); 
                }

这会导致我的 TextView 不会换行,即使我将 TextView 直接包含在 XML 文件中也能正常工作。

结果是这样的: screenshot

编辑: TextView 布局:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TextViewDescription"
    style="@style/AppTheme"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:paddingRight="8dp"
    android:text="Loading data..."
    android:textSize="@dimen/description_size"
    android:layout_weight="1"
    android:ellipsize="none"
    android:scrollHorizontally="false" />

最佳答案

您可能需要将其设置为多行。尝试

setInputType (InputType.TYPE_TEXT_FLAG_MULTI_LINE);

如果您有其他 InputType 属性,则需要将它们“或”在一起。

关于Android:动态添加的 TextView 不换行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20012182/

相关文章:

java - HTTP POST 方法返回状态代码 404

java - JAXB:编码时多态性和忽略类作为类型

android - 使用 NestedScrollView + TextView

java - 为TextView中的文本添加不同的监听器

java - Liferay kaleo 工作流程通知,向特定用户发送电子邮件而不是角色

Android从多个文本文件读取并显示在表格 View 中

android - compileReleaseKotlin 失败并出现 java.lang.ClassNotFoundException : com. sun.tools.javac.util.Context

android - 遍历linux/android中某个目录下的所有文件(包括 '.'开头的文件——隐藏文件)

android - ListView 中的 performItemClick 不突出显示项目

xml - 选择两个节点之间的所有 sibling (不包括 sibling )