Mic*_*eyR 167 layout android refresh
我想强制主布局资源视图重绘/刷新,比如Activity.onResume()方法.我怎样才能做到这一点 ?
通过主布局视图,我的意思是在我的Activity.onCreate()中调用的那个('R.layout.mainscreen'),如下所示: -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*dam 148
要严格回答问题:使用invalidate():
public void invalidate()从:API级别1
使整个视图无效.如果视图可见,将在某个时间点调用onDraw(Canvas).必须从UI线程调用此方法.要从非UI线程调用,请调用postInvalidate().
ViewGroup vg = findViewById (R.id.mainLayout);
vg.invalidate();
Run Code Online (Sandbox Code Playgroud)
现在,当Activity恢复时,它会使每个View都自行绘制.不需要调用invalidate().要应用主题,请确保在绘制任何视图之前(即之前)执行此操作setContentView(R.layout.mainscreen);
public void setTheme(int resid)自:API Level 1
为此上下文设置基本主题.请注意,应该在Context中实例化任何视图之前调用它(例如在调用setContentView(View)或inflate(int,ViewGroup)之前).
API文档参考位于:http://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme%28int%29
由于onDraw()方法适用于已经实例化的视图,因此setTheme将不起作用.我自己没有主题经验,但我能想到的两个备选方案是:
key*_*fer 39
尝试 getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
Moh*_*ULI 34
尝试recreate()它将导致重新创建此活动.
Nav*_*mad 28
解:
伙计们我尝试了所有的解决方案,但它们对我没用,我必须将EditText的setVisibility设置为VISIBLE,然后在ScrollView中可以看到这个EditText ,但是我无法刷新根视图才能生效.我解决了我的问题,当我需要刷新视图时,我将ScrollView可见性更改为GONE,然后再次将其设置为VISIBLE以使其生效,它对我有用.这不是确切的解决方案,但它只能起作用.
private void refreshView(){
mScrollView.setVisibility(View.GONE);
mScrollView.setVisibility(View.VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)
调用invalidate()或postInvalidate()根目录布局显然不能保证重绘子视图.在我的特定情况下,我的根布局是一个TableLayout,并有几个TableRow和TextView类的子代.调用postInvalidate(),或者requestLayout()甚至forceLayout()根TableLayout对象没有造成任何布局要TextViews重绘.
所以,我最终做的是递归地解析布局,寻找那些TextViews,然后调用postInvalidate()每个TextView对象.
代码可以在GitHub上找到:https: //github.com/jkincali/Android-LinearLayout-Parser
否则你也可以试试这个 -
ViewGroup vg = (ViewGroup) findViewById (R.id.videoTitleTxtView);
vg.removeAllViews();
vg.refreshDrawableState();
Run Code Online (Sandbox Code Playgroud)
Far*_*an 5
只需在 onresume 中设置您的内容视图
onResume 内的 setContentView(R.layout.yourview) ..
例子:
@Override
public void onResume(){
super.onResume();
setContentView(R.layout.activity_main);
postIncomeTotals();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
364649 次 |
| 最近记录: |