gpt4 book ai didi

android - 防止文字在 TextView 中被打断

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:06 24 4
gpt4 key购买 nike

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

layout.addView(tv);

最佳答案

问题出在下一行

fullString = fullString.replaceAll(" ", "&#160");

删除它,它将根据您的需要工作,您不需要用 html 代码替换空格,fromHtml 会为您完成

关于android - 防止文字在 TextView 中被打断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30142308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com