`
ljz1318078262
  • 浏览: 40586 次
文章分类
社区版块
存档分类
最新评论

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.



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics