问题:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a
我的场景是
LinearLayout.LayoutParams params11
= new LinearLayout.LayoutParams(264, LinearLayout.LayoutParams.WRAP_CONTENT);
bt21.setLayoutParams(params11);
更新设置UI
问题的意思是更新UI阻塞了主线程
解决方法: 利用runOnUiThread,下面是我一个例子,关于runOnUiThread的用法,自行查询
runOnUiThread(new Runnable() {
@Override
public void run() {
//更改UI;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//TODO todo somthing here
System.out.println("宽度" + bt1.getWidth());
LinearLayout.LayoutParams params1
= new LinearLayout.LayoutParams(bt1.getWidth(),
LinearLayout.LayoutParams.WRAP_CONTENT);
bt2.setLayoutParams(params1);
}
},1 * 1000); //延迟10秒执行
}
});
Android
ie
read
vi
origin
ed
ce
ng
pt
ep
rom
om
id
nl
view
cep
mpl
al
hr
mp
gin
igi
gi
ori
io
le
roi
pl