android字符串zhongdanci,Android: – 如何在单词中断android TextView 6.0 OS以下添加连字符“ – ”...

我实现了另一种解决此问题的方法.

要概括所有设备的实现,请根据句子中最长的单词动态排列文本.请使用以下两种方法并传递完整的句子并使用TextView.这将自动为所有屏幕排列所有设备的文本.

/**

*

* @param message - Raw Header message from Server - Sentance/ Paragraph.

* The message will split and rearrange the size based on its character length

*/

private void updateText(String message, TextView mTvMessageText ) {

try {

if (message == null || message.length() == 0) {

return;

}

String word = getLongestWordLength(message);

if (word == null) {

return;

}

String wordUpper = word.toUpperCase();// Convert the word to uppercase to find the Maximum Space

// mTvMessageText - TextView need to Update the Value

float width = ((mTvMessageText.getMeasuredWidth()) - 120); // Get the width of the View with reduced padding

float textWidth = mTvMessageText.getPaint().measureText(wordUpper); // Get the word Holding Space through Paint

float textSizeInPixel = getResources().getDimension(R.dimen.message_size); // Get dimension text Size - My Size is 65sp

float lineSpacingExtra = getResources().getDimension(R.dimen.message_line_spacing); //High text size required Negative Line Spacing initially -15

/**

* Loop will reduce the font size of actual 3% in each looping

* The looping condition is the longest word in the sentence to hold in a single line of View

* Reduce the Inline space with accordingly

* Submit the reduced amount of size in the textView and check the holding pixels

* If the holding pixels are up above the total pixel size, the loop will continue

*/

while (textWidth > width) {

textSizeInPixel -= textSizeInPixel * (0.03); // Reduce the Fount Size with 3% each looping

lineSpacingExtra += Math.abs(lineSpacingExtra) * (0.06); // Reduce the minus space extra

this.mTvMessageText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeInPixel);

this.mTvMessageText.setLineSpacing(lineSpacingExtra, 1f);

textWidth = mTvMessageText.getPaint().measureText(wordUpper);// Assign value to measure the text Size

}

/**

* M & N devices has a property to rearrange the word with hyphenation

* In Order to avoid the same, Application will add this logic

*/

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

mTvMessageText.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE);

}

/**

* Text Set Using from Html

*/

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

this.mTvMessageText.setText(Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY));

} else {

this.mTvMessageText.setText(Html.fromHtml(message));

}

} catch (Resources.NotFoundException e) {

Log.e(TAG, e.getMessage());

}

}

/**

*

* @param wordString - Raw String with Multiple word

* This may be a header

* May be a paragraph

* May be contain Multiple Paragraphs

* @return - Identify the Longest word and return the length of it

*/

private String getLongestWordLength(String wordString) {

try {

if (wordString == null) {

return null;

}

if (wordString.length() == 0) {

return null;

}

String[] splitArray = wordString.split(" ");

String word = "";

for (int i = 0; i < splitArray.length; i++) {

if (splitArray[i].length() > word.length()) {

word = splitArray[i];

}

}

return word;

} catch (Exception e) {

Log.e(TAG, e.getMessage());

}

return null;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值