android sqlite自定义函数,Android中自定义一个View的方法详解
本文实例讲述了Android中自定义一个View的方法。分享给大家供大家参考,具体如下:
Android中自定义View的实现比较简单,无非就是继承父类,然后重载方法,即便如此,在实际编码中难免会遇到一些坑,我把自己遇到的一些问题和解决方法总结一下,希望对广大码友们有所帮助。
注意点① 用xml定义Layout时,Root element 最好使用merge
当我们需要继承一个布局比较复杂的ViewGroup(比较多的是LinearLayout、RelativeLayout)时,通常会用xml来写布局,然后在自定义的View类中inflate这个定义了layout的xml文件。
首先新建一个名为 MyLayout 的 class 文件,在 init 方法中解析稍后定义的xml文件。
/**
* Created by liangfei on 4/14/15.
*/
public class MyLayout extends LinearLayout {
public MyLayout(Context context) {
super(context);
init();
}
private void init() {
setOrientation(VERTICAL);
View rootView = inflate(getContext(), R.layout.my_layout, this);
((TextView) rootView.findViewById(R.id.title)).setText("MyLayout");
((TextView) rootView.findViewById(R.id.desc)).setText("A customized layout");
}
}
然后新建一个取名为my_layout的布局文件, 并把 Root element 设置成merge。
android:id="@+id/title"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
用 Android SDK 附带的 Monitor 工具查看一下运行时的布局信息。
最顶层是一个FrameLayout,然后是一个LinearLayout,里面有两个TextView,可以看出布局没有冗余。
但是,如果把 Root element 换成 LinearLayout,效果会怎么样呢?
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:id="@+id/title"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
很明显,用 LinearLayout 做 Root element 后,布局多了一个层级,成了影响性能的一个因素。
注意点② 重载子类构造函数时要弄清楚父类做了哪些操作
先从我一个惨痛的教训开始,当时我这样自定义了一个Button:
/**
* Created by liangfei on 4/14/15.
*/
public class MyButton extends Button {
public MyButton(Context context) {
this(context, null);
}
public MyButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
}
乍一看貌似没什么问题,构造函数的调用方式都是正确的,但是无论我怎么修改 MyButton 的属性,显示方式就是不正确。
其实问题就出在Button类在构造函数中使用了一个defStyleAttr, 而我这种写法会忽略掉这个defStyleAttr - com.android.internal.R.attr.buttonStyle,稍读源码就知道了。
@RemoteView
public class Button extends TextView {
public Button(Context context) {
this(context, null);
}
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
public Button(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
后来写代码的时候,我都是看了父类的源码之后才敢这么写,如果不确定就老老实实地写成下面这种形式。
/**
* Created by liangfei on 4/14/15.
*/
public class MyButton extends Button {
public MyButton(Context context) {
super(context);
init();
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
}
其实,还有很多其他的坑,比如 Button 的高度,后面抽时间再总结一下
希望本文所述对大家Android程序设计有所帮助。
android sqlite自定义函数,Android中自定义一个View的方法详解相关推荐
- java去掉字符串中前后空格函数_JAVA中去掉字符串空格各种方法详解
1. String.trim() trim()是去掉首尾空格 2.str.replace(" ", ""); 去掉所有空格,包括首尾.中间 代码如下 复制代码 ...
- php中读取大文件实现方法详解
php中读取大文件实现方法详解 来源: 时间:2013-09-05 19:27:01 阅读数:6186 分享到:0 [导读] 本文章来给各位同学介绍php中读取大文件实现方法详解吧,有需要了解 ...
- python支持向量机回归_Python中支持向量机SVM的使用方法详解
除了在Matlab中使用PRTools工具箱中的svm算法,Python中一样可以使用支持向量机做分类.因为Python中的sklearn库也集成了SVM算法,本文的运行环境是Pycharm. 一.导 ...
- python中update是啥意思_python中update的基本使用方法详解
前言 Python 字典 update()方法用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中. 语法格式 d.update(e) 参数说明 将e中键-值对添加到字典 ...
- python中search用法_Python中的python re.search方法详解
re.search扫描整个字符串并返回第一个成功的匹配,若string中包含pattern子串,则返回Match对象,否则返回None,注意,如果string中存在多个pattern子串,只返回第一个 ...
- python怎么横着输出_对python3中, print横向输出的方法详解
对python3中, print横向输出的方法详解 Python 2 : print打印的时候,如果结尾有逗号,打出来时候不会换行.但是在python3里面就不行了. Python3: 3.0的pri ...
- vue ajax highcharts,在vue项目中引入highcharts图表的方法(详解)
npm进行highchars的导入,导入完成后就可以进行highchars的可视化组件开发了 npm install highcharts --save 1.components目录下新建一个char ...
- python中验证码连通域分割的方法详解
python中验证码连通域分割的方法详解 这篇文章主要给大家介绍了关于python中验证码连通域分割的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需 ...
- Python精讲:在Python中遍历字典的三大方法详解
欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<在Python中遍历字典的三大方法详解>.本知识点主要内容有:使用字典对象的items()方法可以遍历字典的项和字典的&qu ...
最新文章
- 改头换面 OpenSSL将改用新型许可证
- 怎样保证客户端和服务器端数据的一致性(数据的同步)
- capsule 安装_CAPSULE SERVANT安卓版下载-CAPSULE SERVANT手游app安装-菜鸟下载
- 服务器工具个人免费版下载使用,xshell个人免费版,xftp个人免费版
- js实现html页面倒计30秒,javascript实现简单页面倒计时
- 如何设定vs2012用linux文件格式,Visual Studio 2012发布网站详细步骤
- Asp.net core使用MediatR进程内发布/订阅
- 全网最细之抽象类讲解
- shell记录报警系统执行的危险命令
- 教你用canvas绘制矩形
- 工作4年,我从阿里巴巴辞职到了国企
- 无线路由器破解教程-CDlinux(by 星空武哥)
- gamit批量下载精密星历shell脚本
- 微信小程序之各类文件下载保存到本地
- Unity动画 代码加载动画,可复用
- 基于Android 的手机传感器检测
- openwrt 无线基础知识介绍
- 2021年发表心理学论文被引量TOP 10
- 播布客教学视频_C学习笔记_8.1_统计1到100中9的个数(分治)
- java for coun,为什么程序中的一段for循环没有被执行
热门文章
- 比特币黄金BTG遭遇51%算力攻击,即将归零?
- weblogic反序列化漏洞
- C++ Error: no appropriate default constructor available
- Android数据存储之SharedPreferences
- 使tomcat和lighttpd使用service启停
- crossdomain.xml用法
- 《大数据、小数据、无数据:网络世界的数据学术》一 3.3 社会与技术
- js中propertyIsEnumerable()方法使用介绍
- 【mysql】SQL常用指令
- Android Studio实用插件使用