强制TextView多行显示而不使用\n

9

有什么方法可以强制文本视图在视图内的空间不足时换行。我希望它能够自动完成这个行为,而不需要通过编程查找大小并强制换行。

在这段代码中,它会将按钮挤出屏幕。

<TableRow>
                <TextView android:layout_width="fill_parent"
                    android:paddingLeft="5dp" android:paddingRight="5dp"
                    android:singleLine="false" android:layout_height="wrap_content"
                    style="@style/heading1" android:gravity="left" android:id="@+id/store_address"></TextView>

                <TableLayout android:layout_height="wrap_content"
                    android:layout_width="wrap_content" android:gravity="right">
                    <TableRow>
                        <ImageView android:id="@+id/img_user" android:gravity="right"
                            android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:scaleType="fitXY" android:src="@drawable/qrcode"
                            android:paddingBottom="2dp" />
                    </TableRow>
                    <TableRow>
                        <ImageView android:id="@+id/nfc" android:gravity="right"
                            android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:scaleType="fitXY" android:src="@drawable/nfc" />
                    </TableRow>
                </TableLayout>
            </TableRow>

我看不到任何按钮。但我猜你的问题是你将TextView的宽度设置为fill_parent。在Android中,换行是默认行为。但如果你的视图填满整个屏幕,它将一直延伸到屏幕的末端才会换行。请将你的textView缩小到足够小,以便还有空间放置“按钮”。 - pumpkee
我将宽度硬编码了,看起来可以工作。但我不想这样做。 - kgibbon
1
看一下布局权重。它可以让你设置子视图使用父视图的百分比。 - pumpkee
2个回答

10
将以下属性添加到TextView控件:

android:maxWidth = "100dip" 这里我使用了100dip,您可以给出任何尺寸。一旦文本超过指定的宽度,它将自动换到新行。


1
这并不是处理问题最动态的方式,但我根据绘制的行数增加了 TextView 的高度。
        // Create TextViews with Question
        TextView question = new TextView(this);

        if (("Q" + i + ". " + alQuestions.get(i-1)).length() < 70)
        {
            question.setLayoutParams(new TableRow.LayoutParams(0, 75, .9f));
        }
        else if (("Q" + i + ". " + alQuestions.get(i-1)).length() < 110)
        {
            question.setLayoutParams(new TableRow.LayoutParams(0, 100, .9f));
        }
        else if (("Q" + i + ". " + alQuestions.get(i-1)).length() < 150)
        {
            question.setLayoutParams(new TableRow.LayoutParams(0, 125, .9f));
        }

        question.setTextColor(Color.BLUE);
        question.setSingleLine(false);
        question.setEllipsize(TextUtils.TruncateAt.END);
        question.setMaxLines(5);

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