Android 自定义View 使用问题

自定义View 的初始化方式,第一种是通过java代码add到布局上,第二种是通过xml配置到布局中,代码如下:


第一种:

MyView mv=new MyView(this);
CoordinatorLayout coordinatorLayout=(CoordinatorLayout)findViewById(R.id.activityLayout);
coordinatorLayout.addView(mv);
 
 
CoordinatorLayout 为要要添加自定义View 的布局对象

第二种:

<com.example1.MyView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
 
com.example1.MyView 为自定义View所在的包+类名
 
在使用这两种方式进行初始化自定义View发现一个问题,就是自定义View 在使用Java代码进行初始化的时候运行程序是不会报错的,但使用xml配置就会出现程序崩溃的问题,后来查文档发现,如果使用xml配置自定义view控件的时候必须重载一个带有Context,AttributeSet 两个参数的构造方法,官方文档的介绍是 "Constructor that is called when inflating a view from XML.“ 英语能力有限,我的理解是控件通过xml配置的时候会使用该格式的构造方法进行初始化。


其它构造方法的解释如下:
<nobr style="white-space:normal; font-family:Roboto,sans-serif; line-height:16.66666603088379px"><span class="sympad" style="margin-right:2px"><a target="_blank" href="file:///Users/aaronli/Library/Application%20Support/Dash/Versioned%20DocSets/Android%20-%20DHDocsetDownloader/6-0r1/Android.docset/Contents/Resources/Documents/docs/reference/android/view/View.html#View(android.content.Context,%20android.util.AttributeSet,%20int)" style="color:rgb(3,155,229); text-decoration:none; margin-bottom:0px">View</a></span>(<a target="_blank" href="file:///Users/aaronli/Library/Application%20Support/Dash/Versioned%20DocSets/Android%20-%20DHDocsetDownloader/6-0r1/Android.docset/Contents/Resources/Documents/docs/reference/android/content/Context.html" style="color:rgb(3,155,229); text-decoration:none">Context</a>context,<a target="_blank" href="file:///Users/aaronli/Library/Application%20Support/Dash/Versioned%20DocSets/Android%20-%20DHDocsetDownloader/6-0r1/Android.docset/Contents/Resources/Documents/docs/reference/android/util/AttributeSet.html" style="color:rgb(3,155,229); text-decoration:none; margin-bottom:0px">AttributeSet</a>attrs, int defStyleAttr)</nobr>
Perform inflation from XML and apply a class-specific base style from a theme attribute.
<nobr style="white-space:normal; font-family:Roboto,sans-serif; line-height:16.66666603088379px"><span class="sympad" style="margin-right:2px"><a target="_blank" href="file:///Users/aaronli/Library/Application%20Support/Dash/Versioned%20DocSets/Android%20-%20DHDocsetDownloader/6-0r1/Android.docset/Contents/Resources/Documents/docs/reference/android/view/View.html#View(android.content.Context,%20android.util.AttributeSet,%20int,%20int)" style="color:rgb(3,155,229); text-decoration:none; margin-bottom:0px">View</a></span>(<a target="_blank" href="file:///Users/aaronli/Library/Application%20Support/Dash/Versioned%20DocSets/Android%20-%20DHDocsetDownloader/6-0r1/Android.docset/Contents/Resources/Documents/docs/reference/android/content/Context.html" style="color:rgb(3,155,229); text-decoration:none">Context</a>context,<a target="_blank" href="file:///Users/aaronli/Library/Application%20Support/Dash/Versioned%20DocSets/Android%20-%20DHDocsetDownloader/6-0r1/Android.docset/Contents/Resources/Documents/docs/reference/android/util/AttributeSet.html" style="color:rgb(3,155,229); text-decoration:none; margin-bottom:0px">AttributeSet</a>attrs, int defStyleAttr, int defStyleRes)</nobr>
Perform inflation from XML and apply a class-specific base style from a theme attribute or style resource.



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值