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>
更多相关文章
- 安卓开发学习之003 LinearLayout之baseLine详解
- 创建SqliteDatabase
- 在项目中引用android.support.v7
- android 焦点问题
- EditText
- Android(安卓)ListView的item点击无响应的解决方法
- Android自定义控件
- android五种布局模式
- Android.GridView事件监听