自定义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.
分享到:
相关推荐
android 自定义view比较综合的例子,涉及到一些复杂的实现效果,有一些基础的人可以参考下。
android 自定义view及自定义属性
android自定义View实现SwitchView
android 自定义View界面大合集
Android自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View...
仿真书籍翻页效果BookPageView简介:实现了仿真翻页效果,教程完整地描述了翻页原理分析到性能优化的过程教程博客:Android自定义View——从零开始实现书籍翻页效果(一) Android自定义View——从零开始实现书籍...
创建全新的视图将满足我们独特的UI需求。 本文介绍在指南针开发中会用到的罗盘的界面UI,通过继承View类实现的自定义视图,以此来深刻了解自定义视图。
Android中自定义View操作Android中自定义View操作Android中自定义View操作
android自定义View之NotePad出鞘记,自定义NotePad,android自定义笔记本
用自定义View的方式来实现圆形的遥控器菜单。更多关于android自定义的知识可访问:http://blog.sina.com.cn/s/blog_9c4f0e790102vyt3.html
android使用自定义view和自定义button实现的小demo,可以实现button的点击变换背景等功能
根据鸿洋大神博客所写。自己的学习笔记。自定义View实现验证码。
Android自定义ShadowView,可替代CardView使用
Android自定义View——拼手气转盘 Android自定义View——拼手气转盘
android项目下对图片高斯模糊毛玻璃,自定义view,模糊程度可控制,高斯模糊毛玻璃,自定义view,模糊程度可控制
安卓自定义View三滑块SeekBar,可以根据需要变换成双滑块或其他多滑块,可以动态设置三滑块的值
主要为大家详细介绍了Android自定义view实现选座功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Android自定义View——贝塞尔曲线实现直播点赞效果 Android自定义View——贝塞尔曲线实现直播点赞效果
Android自定义View
Android自定义View之进度条Demo,Android自定义View之进度条Demo