SuR*_*aTi 7 android classcastexception android-layout
我创建了一个名为MyDraw的自定义视图,这是我的MyDraw代码,
public class MyDraw extends View {
public MyDraw(Context context) {
super(context);
}
public MyDraw(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyDraw(Context context, AttributeSet attrs) {
super(context, attrs);
}
........................................
}
Run Code Online (Sandbox Code Playgroud)
我使用包名称在XML文件中添加了视图.它工作正常.现在我想在运行时为MyDraw设置高度和宽度,因为我使用了以下代码,
mMyDraw.setLayoutParams(new LayoutParams(220, 300));
Run Code Online (Sandbox Code Playgroud)
但我得到了例外,
java.lang.ClassCastException:android.view.ViewGroup $ LayoutParams
如何解决这个异常?请帮我..
Dim*_*ris 19
您必须覆盖View的onMeasure()方法.
有一个很好的例子,你可以在这里查看:http://kahdev.wordpress.com/2008/09/13/making-a-custom-android-button-using-a-custom-view/
我推荐的一个非常酷的视频是:http://marakana.com/forums/android/general/563.html
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
21097 次 |
| 最近记录: |