gpt4 book ai didi

android - 如何强制整个布局 View 刷新?

转载 作者:IT老高 更新时间:2023-10-28 12:55:11 40 4
gpt4 key购买 nike

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

主布局 View 是指在我的 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();

现在,当 Activity 恢复时,它会让每个 View 都绘制自己。不需要调用 invalidate()。要应用主题,请确保在绘制任何 View 之前执行此操作,即在 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() 方法适用于已经实例化的 View ,因此 setTheme 将不起作用。我自己对主题没有经验,但我能想到的两个替代选择是:

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

关于android - 如何强制整个布局 View 刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5991968/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com