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

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

问题描述

我想迫使主体布局,资源视图重绘/刷新,在说Activity.onResume()方法。我怎样才能做到这一点?

通过主要的布局来看,我所指的是('R.layout.mainscreen下面)被称为在我Activity.onCreate(),像这样: -

 保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.mainscreen);
}
 

任何帮助,很多AP preciated, 米

解决方案

要严格回答这个问题:使用无效():

  

公共无效无效()   自:API级别1

     

无效整个视图。如果视图是可见的,的OnDraw(画布)将调用在将来的某个时候。这必须调用从UI线程。从一个非UI线程中调用,调用postInvalidate()。

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


现在,该活动恢复时,它使每个View绘制自己。应该不需要调用无效()。要应用的主题,确保你做的任何视图绘制之前,即前的setContentView(R.layout.mainscreen);

  

公共无效setTheme(INT渣油)   自:API级别1

     

设置基本主题此背景下。请注意,这应该被称为前的任何意见被实例化的上下文(调用的setContentView(查看之前的例子)或膨胀(INT,ViewGroup中))。

该API文档参考这里:<一href="http://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme%28int%29">http://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme%28int%29

由于的OnDraw()方法适用于已经实例化的意见,setTheme将无法正常工作。我有自己的主题没有任何经验,但有两个备选方案我能想到的是:

  1. 在调用setTheme中的onCreate()来替代,或者
  2. 重做的setContentView(R.layout.mainscreen);强制所有重新实例布局。

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

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);
}

Any help, much appreciated, M.

解决方案

To strictly answer the question: Use invalidate():

public void invalidate () Since: API Level 1

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();


Now, when the Activity resumes, it makes every View to draw itself. No call to invalidate() should be needed. To apply the theme, make sure you do it before any View is drawn, i.e., before setContentView(R.layout.mainscreen);

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

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)).

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

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. call setTheme in onCreate() instead, or
  2. redo setContentView (R.layout.mainscreen); to force reinstantiate all the layout.

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

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