安卓:动态添加的TextView不能自动换行

3
动态将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); 
                }

这导致我的文本视图不会自动换行,即使如果我直接在XML文件中包含TextView,它也可以完美地工作。
结果如下: 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" />
5个回答

0

对于这个问题...

当你拥有了

<TableLayout>
  <TableRow>
    <TextView>

    </TextView>
  </TableRow>
</TableLayout>

为了使文本不被截断,添加

android:layout_weight="1"

在TextView内部

<TextView>

</TextView>

0
在你的代码的这一部分,你正在获取你的TextView数组:
ArrayList<TextView> textViews = des.getText();

我们并不清楚那些 TextView 是如何创建的。但我猜想你没有为 TextView 设置 single_linemax_lines

在将你的 TextView 添加到 LinearLayout 时,请尝试使用以下代码:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

while (iter.hasNext()) {
    TextView textView = iter.next();
    description.addView(textView, params);
}

注意: 创建你的TextView时请使用以下布局(我已经删除了一些属性和样式):

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TextViewDescription"
    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"/>

编辑2:layout_width参数改为wrap_content。请参阅上面代码中的更新参数。(这对我有效)

<LinearLayout android:id="@+id/LinearLayout_Description"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <!-- Dynamically added TextView here -->
</LinearLayout>

TextView textView = (TextView) inflater.inflate(R.layout.textview_description, null); textView.setText(e.text()); 这是我用来创建TextView的代码。我已经添加了LayoutParams,但仍然得到相同的结果。 - jameslfc19
我已经编辑了我的答案,同时在向LinearLayout添加textViews时也更改了Params参数。 - Devrim
好的,即使我将TextView硬编码到LinearView中,文本也没有换行。但我无法弄清原因。 - jameslfc19

0

尝试使用除TableLayout之外的其他布局方式。我对TableLayout并不是很熟悉,但我认为它需要子视图具有相对较短和一致的宽度。

相反,尝试使用LinearLayout,像这样:

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

    <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..." />

    <!-- Dynamically added TextView here -->

</LinearLayout>

然后使用几乎相同的代码添加视图。请注意,我更改了支持添加的父视图——outerLayout。

View view = inflater.inflate(R.layout.description_card, null);

TextView outerView = (TextView) view.findViewById(R.id.LinearLayout_InfoShow);
ArrayList<TextView> textViews = des.getText();

Iterator<TextView> iter = textViews.iterator();
while(iter.hasNext()) {
  TextView textView = iter.next();
  outerView.addView(textView); 
}

0

找到原因了。

布局参数需要一个ViewGroup才能生效。 我不得不改变传递文本给迭代器的方式,但基本上这就是需要改变的:

TextView textView = (TextView) inflater.inflate(text.getViewID(), description, false);

0

你可能需要将它设置为多行。尝试一下:

setInputType (InputType.TYPE_TEXT_FLAG_MULTI_LINE);

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

这似乎不能解决问题。可能与布局宽度有关吗?(已更新主贴) - jameslfc19
我认为你现在最好的选择是简化。例如,尝试将TextView直接放在XML文件中,在你说“动态添加”的地方,并注释掉迭代器循环。如果你能让这个工作起来,那么你就可以开始看看两者之间的区别了。 - Peri Hartman
好的,我建议采取完全不同的方向。我怀疑问题在于TextView不知道它有多少宽度(因为TableLayout根据子视图的要求来确定宽度),所以它只是把所有内容放在一行上,认为这没问题。我马上会发布第二个答案。 - Peri Hartman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接