gpt4 book ai didi

android - 如何防止 URL 被 TextView 包裹?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:44:50 29 4
gpt4 key购买 nike

我有一个多行 TextView,它可以显示一些可选的 URL。现在我遇到了一个问题:我的一些长 URL 显示在 ://

的位置
sometext sometext http://   <-- AUTO LINE WRAP
google.com/

如何禁用整个 URL 或至少 http(s):// 前缀的换行?但是,我仍然需要启用文本换行。

我的文字应该这样换行

sometext sometext    <-- AUTO LINE WRAP
http://google.com/

最佳答案

这只是为 textview 实现自定义换行的概念证明。您可能需要根据您的要求添加/编辑条件。

如果您的要求是我们的 textview 类必须以不以某些文本结尾的方式显示多行(此处为 http://和 http:),我已经通过 SO 修改了非常流行的 textview 类的代码以满足此要求:资源 : Auto Scale TextView Text to Fit within Bounds

变化:

 private boolean mCustomLineWrap = true;

/**
* Resize the text size with specified width and height
* @param width
* @param height
*/
public void resizeText(int width, int height) {
CharSequence text = getText();
// Do not resize if the view does not have dimensions or there is no text
if(text == null || text.length() == 0 || height <= 0 || width <= 0 || mTextSize == 0) {
return;
}

// Get the text view's paint object
TextPaint textPaint = getPaint();

// Store the current text size
float oldTextSize = textPaint.getTextSize();
// If there is a max text size set, use the lesser of that and the default text size
float targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize;

// Get the required text height
int textHeight = getTextHeight(text, textPaint, width, targetTextSize);

// Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes
while(textHeight > height && targetTextSize > mMinTextSize) {
targetTextSize = Math.max(targetTextSize - 2, mMinTextSize);
textHeight = getTextHeight(text, textPaint, width, targetTextSize);
}



if(mCustomLineWrap ) {

// Draw using a static layout
StaticLayout layout = new StaticLayout(text, textPaint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false);
// Check that we have a least one line of rendered text
if(layout.getLineCount() > 0) {
String lineText[] = new String[layout.getLineCount()];
// Since the line at the specific vertical position would be cut off,
// we must trim up to the previous line
String wrapStr = "http:", wrapStrWithSlash = "http://";
boolean preAppendWrapStr = false, preAppendWrapStrWithSlash = false ;
for(int lastLine = 0; lastLine < layout.getLineCount(); lastLine++)
{
int start = layout.getLineStart(lastLine);
int end = layout.getLineEnd(lastLine);
lineText[lastLine] = ((String) getText()).substring(start,end);
if(preAppendWrapStr)
{
lineText[lastLine] = "\n" + wrapStr + lineText[lastLine];
preAppendWrapStr = false;
}
else if(preAppendWrapStrWithSlash)
{
lineText[lastLine] = "\n" + wrapStrWithSlash + lineText[lastLine];
preAppendWrapStrWithSlash = false;
}

if(lineText[lastLine].endsWith(wrapStr))
{
preAppendWrapStr = true;
lineText[lastLine] = lineText[lastLine].substring(0,lineText[lastLine].lastIndexOf(wrapStr));
}
if( lineText[lastLine].endsWith(wrapStrWithSlash))
{
preAppendWrapStrWithSlash = true;
lineText[lastLine] = lineText[lastLine].substring(0,lineText[lastLine].lastIndexOf(wrapStrWithSlash));
}

}
String compString = "";
for(String lineStr : lineText)
{
compString += lineStr;
}
setText(compString);
}

}

// Some devices try to auto adjust line spacing, so force default line spacing
// and invalidate the layout as a side effect
textPaint.setTextSize(targetTextSize);
setLineSpacing(mSpacingAdd, mSpacingMult);

// Notify the listener if registered
if(mTextResizeListener != null) {
mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize);
}

// Reset force resize flag
mNeedsResize = false;
}

关于android - 如何防止 URL 被 TextView 包裹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14871539/

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