作者:手机用户2602906305_849 | 来源:互联网 | 2023-05-16 18:55
HowdoIpreventawordinaTextViewfrombreakinginhalfwhenitdoesntfitonthecurrentlinea
How do I prevent a word in a TextView
from breaking in half when it doesn't fit on the current line and instead move to the next line.
我如何在一个textview中阻止一个单词在不符合当前行的情况下将其分成两半,然后移到下一行。
|android goo|
|gle kitkat |
should instead be
应该
|android |
|google |
|kitkat |
The TextView
is currently added to a RelativeLayout
using this code:
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 = "" + user + ": " + text;
fullString = fullString.replaceAll("\n", "
");
fullString = fullString.replaceAll(" ", "&#160");
tv.setText(Html.fromHtml(fullString));
tv.setTextSize(32f);
tv.setTextColor(getResources().getColor(R.color.text));
layout.addView(tv);
4 个解决方案