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。

更多相关文章

  1. android 你或许不知道的屏幕适配
  2. Android——RIL 机制源码分析
  3. Android(安卓)Layout 之 RelativeLayout RelativeLayout.LayoutP
  4. 『转』Android(安卓)推送方式
  5. Android(安卓)SQLite分析
  6. 关于android UI布局自适应
  7. 从iOS角度思考Android
  8. 魅族note(电信版)缩略图显示不正确问题
  9. android style的继承方式 点(.)和parent

随机推荐

  1. Android(安卓)ImageView的scaleType属性
  2. 使用命令行签名Android应用程序
  3. Android(安卓)Framework 框架系列之 Alar
  4. Android系统源码极速搜索引擎(OpenGrok)
  5. context 的理解
  6. Android(安卓)屏幕截图
  7. Android——你可能不知道的Debug细节
  8. Android资源文件xml的编写
  9. 如何防止android Toast重复显示?
  10. 读取android手机流量信息