强制视图重绘自身 [英] Force a View to redraw itself
问题描述
我已经创建了一个自定义视图(姑且称之为 MyView的
),这基本上只是用绘制在画布上本身的一些文字。要绘制的文本使用全局变量被设置
在程序的执行过程中的某个时刻,我想改变全局变量,并具有 MyView的
重绘自身更新的文本。我试过 findViewById()
然后无效()
,但是这并没有什么。我怀疑,因为在任何的 MyView的
发生了变化,它认为它没有理由叫的OnDraw()
。有没有什么办法来迫使一查看,即使它认为它并不需要重绘自己?
如果我有一个存储的文本MyView的内部成员变量,并创建一个公共制定者,那么只需调用该方法会导致MyView的重绘自身
设置查看里面的变量
将不会调用视图中的一个平局。事实上,同样没有查看系统知道,也不关心内部变量。
调用无效()
在View使其通过视图中绘制本身。你应该看看这个: http://developer.android.com/guide /topics/ui/custom-components.html 。
A 的TextView
在内部,当你调用无效本身的setText()
,并用新文本通过设置重绘自己的setText()
通话。你也应该做同样的事情。
I have created a custom View (let's call it MyView
) which basically just draws some text on itself using the canvas. The text to be drawn is set using a global variable.
At some point during the program's execution, I want to change the global variable, and have the MyView
redraw itself to update the text. I tried findViewById()
and then invalidate()
, but this does nothing. I suspect that since nothing within the MyView
has changed, it thinks it has no reason to call onDraw()
. Is there any way to force a View to redraw itself even if it thinks it doesn't need to?
If I have a member variable inside the MyView that stores the text, and create a public setter for it, then just calling that method causes the MyView to redraw itself
Setting a variable inside the View
will not invoke a draw on the View. In fact, neither does the view system know nor care about internal variables.
Invoking invalidate()
on a View causes it to draw itself via the View. You should check this out: http://developer.android.com/guide/topics/ui/custom-components.html.
A TextView
internally invalidates itself when you invoke setText()
and redraws itself with the new text set via the setText()
call. You should also do something similar.
这篇关于强制视图重绘自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!