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