Android中自定义控件

自定义控件的一个要注意的问题是,自定义控件必须要实现一个参数列表为(Context context, AttributeSet attrs, Map inflateParams)的构造方法。否则将会在引用页面时抛出一个类似于 An error has occured in Process com.android.Shell. Unable to start activity ComponentInfo(com.android.Shell/com.android.Shell.StartShell|;
android.view.ViewInflate$InflateException: Binary XML file Line# 7: Error inflating class CoolView

的异常。原因可能是系统会自动调用自定义控件类中拥有该三个参数的构造方法。如果不定义,则系统无法找到

然后在页面中以class的方式引用即可。如

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
id="@+id/shelllayout" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#003333"
>
<AutoCompleteTextView id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/search_term"
android:completionThreshold="1"
android:selectAllOnFocus="true" />

<view id="@+id/mapview"
class="com.google.android.maps.MapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<com.android.neu.test.CoolView id="@+id/firstview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

更多相关文章

  1. 安卓开发学习之003 LinearLayout之baseLine详解
  2. 创建SqliteDatabase
  3. 在项目中引用android.support.v7
  4. android 焦点问题
  5. EditText
  6. Android(安卓)ListView的item点击无响应的解决方法
  7. Android自定义控件
  8. android五种布局模式
  9. Android.GridView事件监听

随机推荐

  1. android:shape的使用
  2. android 多媒体之 MediaStore 学习记录
  3. 在android中处理各种触摸事件
  4. Unity3D Android播放视频
  5. android HttpClient网络通信工具类基于XM
  6. Android(安卓)震 动
  7. Error:Execution failed for task ':app:
  8. Android下使用C语言的四种编译方式
  9. Android下openGL操作矩阵的函数
  10. Android(安卓)调用系统播放器