如何防止TextView
中的单词在不适合当前行时折断一半,而是移动到下一行。防止Word在TextView中被破坏
|android goo|
|gle kitkat |
应改为
|android |
|google |
|kitkat |
的TextView
使用此代码目前加入RelativeLayout
:
TextView tv = new TextView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (16*scale + 0.5f);
tv.setPadding(dpAsPixels, 0, dpAsPixels, 0);
tv.setLayoutParams(layoutParams);
String fullString = "<b>" + user + ":</b> " + text;
fullString = fullString.replaceAll("\n", "<br />");
fullString = fullString.replaceAll(" ", " ");
tv.setText(Html.fromHtml(fullString));
tv.setTextSize(32f);
tv.setTextColor(getResources().getColor(R.color.text));
layout.addView(tv);
你给出的例子实际上会移到下一行。它只有在大字的情况下,比如15个字符奇怪的(或其他语言),我面临着你面临的问题。 或者你在给出的例子中也遇到过这个问题? – Droidekas
查看:https://github.com/grantland/android-autofittextview –
你试过'android:minLines'吗? – Droidekas