- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个调整字体大小的 TextView,以便 View 中的文本自动扩展(或收缩)以填充 View 的整个宽度。我想我可以通过创建一个自定义的 TextView 来做到这一点,它覆盖 onDraw() 如下:
public class MaximisedTextView extends TextView {
// (calls to super constructors here...)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
TextPaint textPaint = this.getPaint();
Rect bounds = new Rect();
String text = (String)this.getText(); // current text in view
float textSize = this.getTextSize(); // current font size
int viewWidth = this.getWidth() - this.getPaddingLeft() - this.getPaddingRight();
textPaint.getTextBounds(text, 0, text.length(), bounds);
int textWidth = bounds.width();
// reset font size to make text fill full width of this view
this.setTextSize(textSize * viewWidth/textWidth);
}
}
但是,这会使应用程序陷入无限循环(文本大小每次都会略微增大和缩小!),所以我显然采用了错误的方式。对 setTextSize() 的调用是否会触发无效,以便无休止地再次调用 onDraw?
有什么方法可以阻止递归调用(如果发生这种情况)?还是我应该以完全不同的方式进行处理?
最佳答案
Does the call to setTextSize() trigger an invalidate so that onDraw is called again, endlessly?
是的,这可能就是正在发生的事情。如果你take a look of the source code setTextSize
你会看到它会调用这个方法:
private void setRawTextSize(float size) {
if (size != mTextPaint.getTextSize()) {
mTextPaint.setTextSize(size);
if (mLayout != null) {
nullLayouts();
requestLayout();
invalidate(); // yeahh... invalidate XD
}
}
}
所以,如果您正在努力覆盖 onDraw
方法,为什么不直接使用一些 drawText
呢? Canvas
的方法类(class)?
关于android - TextView.onDraw 导致死循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3207414/
我的 php/mysql 脚本有问题。它应该只输出 while 循环一次,但我得到了无限循环和无休止的页面。 $query = mysql_query("SELECT * FROM users WHE
我又来了!所以,我正在用 C++ 开发一个 GBC 模拟器,但我遇到了一些问题。首先,我在 VS10 中使用 Qt,到目前为止它运行良好。但是,我有我的 GUI(主窗口)和一些对象(QListWidg
所以这是我正在做的同一个拖放游戏项目,但是我遇到了一个无限循环问题,我想在其中使用 while(backpackLength 0) { document.getElem
我已经花了 3 个小时试图让这段代码工作,但是每当我尝试时我都会陷入循环并且控制台不断循环。我已经尝试了所有方法 - 我创建了一个只返回 i 并重新分配值的函数,但它似乎不起作用。 出于某种原因,每当
我使用 lua 接口(interface)在我的 C# 程序中获得 lua 支持,如果用户提交这样的代码,工作线程将卡住 while true do end 我有一种方法可以检测无限循环是否正在运行,
这个问题在这里已经有了答案: How does a for loop work, specifically for(;;)? (6 个答案) 关闭 7 年前。 虽然我有一些 Java 经验,但下面的
我有问题。我需要让一个程序在后台运行。该程序用于收集数据并将其保存在我的数据库中。 我开始这样做了: func main() { for { doAll() } } 一次从所有
当我在 Internet Explorer 10 中查看代码时,我收到以下错误(它不一定以标准模式呈现,由于页面的服务方式,这超出了我的控制范围)。 http://errors.angularjs.o
我在 servlet 中遇到了一些问题,每次我更改下拉菜单中的选项时,一个不同的值将传递给 servlet,然后它会导致无限循环。当我没有更改下拉列表中的选项(值没有变化)时,没有错误。 这是我的代码
iOS8 中引入了可自动调整大小的表格 View 单元格(WWDC Session 226 What's new in table and collection views)。在我的项目中,我正在尝试
我是一名优秀的程序员,十分优秀!