强制布局(),请求布局()
我对android文档的阅读发现了方法forceLayout()(用于在下一个布局请求时生成布局显示)和requestLayout()(应该立即发布布局请求),但我无法让它们表现出来正如广告所言。特别是,如果我在 Thread.Sleep 之前设置一组文本,然后在 Thread.Sleep 之后设置一组文本,则它会等待 Sleep 完成,然后再一次设置这两个文本,无论我是否在其间调用 forceLayout() 和 requestLayout() 。请不要回复很多关于我不应该在 UI 线程中调用 Thread.Sleep 的废话。如果我将 Thread.Sleep 包装在 CountDownTimer 中,它就可以很好地工作(只要我的滴答时间足够短,不会干扰睡眠时间,并且计时器的持续时间足够长,以允许睡眠完成。以下是一个例子:
int i=0;
TextView tv2;
TextView tv1;
LinearLayout ll;
Button bt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
tv1=new TextView(this);
tv2=new TextView(this);
bt=new Button(this);
bt.setText("Press to start");
ll.addView(bt);
ll.addView(tv1);
ll.addView(tv2);
tv2.setText("");
setContentView(ll);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tv1.setText("starting sleep");
new CountDownTimer(6000,50){
public void onTick(long msuf)
{if(i==1)
{
try{
Thread.sleep(4000);
tv2.setText("waking up");
}
catch(InterruptedException e){};
}
i++;
}
public void onFinish(){}}.start();
}
});
}
My reading of the android documentation finds the methods forceLayout() (which is to produce a layout display at the next layout request) and requestLayout() (which is supposed to post an immediate layout request), but I can not get them to behave as advertised. In particular, if I do one set text before a Thread.Sleep and one after, it waits for the Sleep to finish before setting both texts at once, whether or I call the forceLayout() and requestLayout() in between. Please do not respond with a lot of nonsense about how I should not call a Thread.Sleep in the UI thread. If I wrap the Thread.Sleep in a CountDownTimer it works perfectly well (as long as I have the tick time short enough to not interfere with sleep time, and the duration of the timer long enough to permit the Sleep to finish. The following is an example:
int i=0;
TextView tv2;
TextView tv1;
LinearLayout ll;
Button bt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
tv1=new TextView(this);
tv2=new TextView(this);
bt=new Button(this);
bt.setText("Press to start");
ll.addView(bt);
ll.addView(tv1);
ll.addView(tv2);
tv2.setText("");
setContentView(ll);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tv1.setText("starting sleep");
new CountDownTimer(6000,50){
public void onTick(long msuf)
{if(i==1)
{
try{
Thread.sleep(4000);
tv2.setText("waking up");
}
catch(InterruptedException e){};
}
i++;
}
public void onFinish(){}}.start();
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[关于在 UI 线程中调用
sleep()
的很多废话]。如果我没理解错的话,你的意思是这样的:如果你让主线程休眠,它就会停止处理任何东西:当前方法、线程循环和消息队列。再次唤醒后,它将完成该方法的执行,第二个
setText()
会覆盖第一个,然后让线程循环继续并刷新 UI,仅显示第二个文本。requestLayout()
和forceLayout()
实际上都不能让 UI 立即刷新,它们都会在线程循环中调度布局请求。我不确定,但我认为它们之间的区别在于requestLayout()
是由已更改其父级中的大小/位置的视图调用的,并且 < code>forceLayout() 由需要重新布局其子级的ViewGroup
调用。因此[关于在 UI 线程中调用
sleep()
的更多废话]。对于此类事情,如果您不想搞乱多线程,那么在主线程处理程序上调用postDelayed()
可能是最好的解决方案。[lots of nonsense about calling
sleep()
in UI thread]. If i get it right, you mean having something like:If you make your main thread sleep, it will just stop processing anything: the current method, the thread loop and the message queue. Once awake again, it will finish executing the method, with the second
setText()
overriding the first one, and then leave the thread loop continue and do the UI refresh, showing only the second text.Not
requestLayout()
norforceLayout()
can actually make the UI refresh immediately, they will both schedule a layout request in the thread loop. I'm not sure, but I think the difference between them is thatrequestLayout()
is called by a view that has changed its size/position in its parent, andforceLayout()
is called by aViewGroup
that needs its children to be re-laid out.Therefore [more nonsense about calling
sleep()
in UI thread]. For such things callingpostDelayed()
on a main thread handler is the best solution probably, if you don't want to mess with multithreading.