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. Android跳转到应用商店详情页面
  2. 【Android】内嵌html页面与native代码简单交互
  3. Android06_Android中常用控件
  4. 软键盘默认不弹出,点击别的控件让EditText获得焦点并弹出软键盘
  5. Android基本控件和事件以及消息总结
  6. android中控件点击两次才响应onclick方法
  7. Android 页面跳转(无/含有返回结果)

随机推荐

  1. Android牟利之道(二)--广告平台的介绍
  2. Android中实现跨进程通信(IPC)的方式(三)
  3. Study on Android【三】--Intent消息传递
  4. Android开发人员终于在“app-构建控制台
  5. 为什么要用Android(安卓)Studio?
  6. Android中显示网络上的图片
  7. android重力感应模拟器sensorsimulator
  8. [置顶] android 数据库版本升级
  9. Android上HDMI介绍(基于高通平台)
  10. Android机器调整屏幕方向