强制视图重绘自身 [英] Force a View to redraw itself

查看:175
本文介绍了强制视图重绘自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个自定义视图(姑且称之为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆