android 定制 View派生类
android UI有两种实现方式,一种是直接硬编码在代码中,在代码中做UI的排版布局,另一种是把UI布局在xml中。由于我需要在View上自己画一些曲线,所以直接从View类派生。一开始使用setContentView( new CustomeView())的方式使用,能正常的画点画线。最后我把CustomeView和其它android自带TextView等View的派生类放在xml中使用时,并通过setContentView( resourceID)方式设置activity的UI,运行时 在logcat中看到虚拟机找不到CustomeView这个类。最后发现是要在xml中加上整个package name(如红色所示),因为自定义的派生类不再java虚拟机的classpath中。
<com.practice.fundtest. CustomeView
android:id="@+id/fundView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
当CustomeView类能正常加载后,我用findViewById形式想得到CustomeView的实例对象,但每次findViewById类都返回null。用findViewById去找android自带的View对象都能正确获得,由此想到是不是在setContentView中inflate xml时,没有把id值传给CustomeView的实例,最后发现很多在xml中的属性都会通过View的构造函数 View(Context context, AttributeSet attr)中的第二个参数传递进来的,而我的派生类只提供了 CustomeView(Context context)的够咱函数,导致id值无法传入,findViewById返回null。
更多相关文章
- android 你或许不知道的屏幕适配
- Android——RIL 机制源码分析
- Android(安卓)Layout 之 RelativeLayout RelativeLayout.LayoutP
- 『转』Android(安卓)推送方式
- Android(安卓)SQLite分析
- 关于android UI布局自适应
- 从iOS角度思考Android
- 魅族note(电信版)缩略图显示不正确问题
- android style的继承方式 点(.)和parent