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>

转载于:https://www.cnblogs.com/hujj/archive/2012/03/16/2399606.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值