将TextView强制转换为mutiline \ n
问题描述:
任何想法如何强制textview在视图内的空间用完时转到新行。我想要发生的行为是,如果没有以编程方式查找大小并强制换行,我希望它自己发生。将TextView强制转换为mutiline n
在此代码中,它强制按钮离开屏幕。
<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
控制:
android:maxWidth = "100dip"
在这里,我已经使用100dip,你可以给你想要的任何尺寸。一旦文本超过指定的宽度,它将自动移动到一个新的行。
+0
+1确实有效,与\ n相反 – Gangnus 2012-01-23 10:45:27
答
这不是处理问题的最有活力的方式,但我根据绘制的线数增加了高度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);
我看不到任何按钮。但我想你的问题是你设置TextView的宽度fill_parent。打破新行是android中的默认行为。但是如果你的视图填满了整个屏幕,它将不会中断,直到屏幕结束。让你的textView足够小,以便“按钮”仍然有空间。 – pumpkee 2011-05-15 06:51:41
我对宽度进行了硬编码,看起来像是一个字。我宁愿不这样做。 – kgibbon 2011-05-15 06:56:37
看看布局权重。它允许您设置子视图使用多少百分比的父视图。 – pumpkee 2011-05-15 07:05:34