问题

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

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

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


解决方法

要严格回答问题:使用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();

现在,当活动恢复时,它使每个视图来绘制自己.不需要调用invalidate().要应用主题,请确保在绘制任何视图之前,即在 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)).

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

由于onDraw()方法适用于已实例化的视图,setTheme将无法工作.我自己没有经验的主题,但我可以考虑的两个替代选项:

  1. call setTheme in onCreate() instead, or
  2. redo setContentView (R.layout.mainscreen); to force reinstantiate all the layout.



相关问题推荐