阻止Word在TextView中被破坏

Osk*_*son 11 android textview

TextView当它不适合当前行而是移动到下一行时,如何防止一个单词中断一半.

|android goo|
|gle kitkat |
Run Code Online (Sandbox Code Playgroud)

应该是

|android    |
|google     |
|kitkat     |
Run Code Online (Sandbox Code Playgroud)

在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(" ", "&#160");
tv.setText(Html.fromHtml(fullString));
tv.setTextSize(32f);
tv.setTextColor(getResources().getColor(R.color.text));

layout.addView(tv);
Run Code Online (Sandbox Code Playgroud)

Vil*_*len 5

问题在于以下几点

fullString = fullString.replaceAll(""," ");

删除它,它将按你的需要工作,你不需要用html代码替换空间,fromHtml将为你做