安卓UI setLayoutParams 注意事项

先看代码

LinearLayout layout=new LinearLayout(this);
TextView tv=new TextView(this);
ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(100,100);
tv.setLayoutParams(params);
layout.addView(tv);

这个代码是没问题的,接着看下一个

LinearLayout layout=new LinearLayout(this);
TextView tv=new TextView(this);
ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(100,100);
layout.addView(tv);
tv.setLayoutParams(params);

上面这个代码是有问题的,会抛出 ClassCastException ViewGroup.LayoutParams不能强转为LinearLayout.LayoutParams。

查看更多请移步

猜你喜欢

转载自blog.csdn.net/hellomypeople/article/details/78634459