如何强制整个布局视图刷新? [英] How to force an entire layout View refresh?

查看:23
本文介绍了如何强制整个布局视图刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强制主布局资源视图重绘/刷新,比如 Activity.onResume() 方法.我怎样才能做到这一点 ?

I want to force the main layout resource view to redraw / refresh, in say the Activity.onResume() method. How can I do this ?

通过主布局视图,我的意思是在我的 Activity.onCreate() 中调用的那个(下面的R.layout.mainscreen"),如下所示:-

By main layout view, I mean the one ('R.layout.mainscreen' below) that is called in my Activity.onCreate(), like this:-

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainscreen);
}

推荐答案

严格回答问题:使用invalidate():

To strictly answer the question: Use invalidate():

public void invalidate()从:API 级别 1

public void invalidate () Since: API Level 1

使整个视图无效.如果视图可见, onDraw(Canvas) 将在未来的某个时间点被调用.这必须从 UI 线程调用.要从非 UI 线程调用,请调用 postInvalidate().

Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().

ViewGroup vg = findViewById (R.id.mainLayout);
vg.invalidate();

<小时>

现在,当 Activity 恢复时,它会让每个 View 自己绘制.不需要调用 invalidate() .要应用主题,请确保在绘制任何 View 之前执行此操作,即在 setContentView(R.layout.mainscreen);

public void setTheme (int resid)从:API 级别 1

public void setTheme (int resid) Since: API Level 1

为此上下文设置基本主题.请注意,这应该在 Context 中实例化任何视图之前调用(例如在调用 setContentView(View) 或 inflate(int, ViewGroup) 之前).

Set the base theme for this context. Note that this should be called before any views are instantiated in the Context (for example before calling setContentView(View) or inflate(int, ViewGroup)).

API 文档参考在这里:http://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme%28int%29

The API doc reference is here: http://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme%28int%29

由于 onDraw() 方法适用于已经实例化的视图,所以 setTheme 将不起作用.我自己没有主题经验,但我能想到的两个替代选项是:

Since the onDraw() method works on already instantiated Views, setTheme will not work. I have no experience with themes myself, but two alternative options I can think are:

  1. 改为在 onCreate() 中调用 setTheme,或者
  2. redo setContentView (R.layout.mainscreen);强制重新实例化所有布局.

这篇关于如何强制整个布局视图刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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