原帖地址:http://my.oschina.net/wangjunhe/blog/99764

创建新的控件:

作为一个有创意的开发者,你经常会遇到安卓原生控件无法满足你的需求。

为了优化你的界面和工作流程,安卓允许你去继承已经存在的控件或者实现你自己的控件。

那么最好的方式去创建一个新的控件是什么?  这主要取决你想要完成什么。

1.有些基本功能原生控件都能提供,所以这个时候你只需要继承并对控件进行扩展。通过重写它的事件,onDraw,但是始终都保持都父类方法的调用。

2.组合控件 就是通过合并几个控件的功能来生成一个控件。

3.完完整整创建一个新的控件。

1.修改存在的控件

例子:

public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet ats, int defStyle) {
super(context, ats, defStyle);
} public MyTextView(Context context) {
super(context);
} public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
public void onDraw(Canvas canvas) {
// 在画布上画文本之下的内容 // 保证默认的文本渲染
super.onDraw(canvas); // 在画布上画文本之上的内容 } @Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
// 写自己的控制 // 保持父类默认的控制
return super.onKeyDown(keyCode, keyEvent);
}
}

2.组合控件

1.最简单的方式,是定义了XML布局文件,然后用include实现重用。(。。。这也算啊。。。)

2.去合并一个控件 通常你自定义的控件需要继承一个ViewGroup(通常就是Layout),就像:

public class MyCompoundView extends LinearLayout {
public MyCompoundView(Context context) {
super(context);
} public MyCompoundView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}

就像activity,比较好的设计一个混合的控件UI布局是使用一个外部的layout资源。

这里我们模拟定义一个:

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”>
<EditText
android:id=”@+id/editText”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
/>
<Button
android:id=”@+id/clearButton”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Clear”
/>
</LinearLayout>

 然后在构造函数初始化的时候:

public class ClearableEditText extends LinearLayout {

    EditText editText;
Button clearButton; public ClearableEditText(Context context) {
super(context); // Inflate the view from the layout resource.
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li; li = (LayoutInflater) getContext().getSystemService(infService);
/*这句很关键,解析反射资源文件,然后将布局附加到当前的控件,也就是this*/
li.inflate(R.layout.clearable_edit_text, this, true); /* 因为反射成功后的布局已经附加上了,那么直接可以findViewById*/
editText = (EditText) findViewById(R.id.editText);
clearButton = (Button) findViewById(R.id.clearButton); // 下面自定义的方法就是为控件注册监听,不解释了
hookupButton();
}
}

使用:在activity_main.xml

<com.example.customview.MyCompoundView android:layout_width="match_parent" android:layout_height="wrap_content" />

3. 完完全全自定义控件

通常是继承View或者SurfaceView ,View类提供一个Canvas(画布)和一系列的画的方法,还有Paint(画笔)。使用它们去创建一个自定义的UI。你可以重写事件,包括屏幕接触或者按键按下等等,用来提供与用户交互。

1.如果你不需要快速重画和3D图像的效果,那么让View作为父类提供一个轻量级的解决方案。

2.如若不然,就需要使用SurfaceView作为父类,这样你就可以提供一个后台线程去画和使用OPENGL去实现你的图像。这个就相对重量级了,如果你的视图需要经常更新,然后由需要显示比较复杂的图像信息(尤其是在游戏和3D可视化),SurfaceView将是更好的选择。

在这里我们讨论前者,后者后期再讨论。

一般你需要重写2个方法: 
1.onMeasure

什么是onMeasure?

下面转载一段文章:

View在屏幕上显示出来要先经过measure(计算)和layout(布局). 
1、什么时候调用onMeasure方法?  
当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec. 
这两个参数指明控件可获得的空间以及关于这个空间描述的元数据.

更好的方法是你传递View的高度和宽度到setMeasuredDimension方法里,这样可以直接告诉父控件,需要多大地方放置子控件.

widthMeasureSpec和heightMeasureSpec这2个参数都是整形是出于效率的考虑,所以经常要做的就是对其解码=>

  1. int specMode = MeasureSpec.getMode(measureSpec);
  2. int specSize = MeasureSpec.getSize(measureSpec);
    1. 依据specMode的值,(MeasureSpec有3种模式分别是UNSPECIFIED, EXACTLY和AT_MOST)
    2. 如果是AT_MOST,specSize 代表的是最大可获得的空间; 
      如果是EXACTLY,specSize 代表的是精确的尺寸; 
      如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。 
      2、那么这些模式和我们平时设置的layout参数fill_parent, wrap_content有什么关系呢? 
      经过代码测试就知道,当我们设置width或height为fill_parent时,容器在布局时调用子 view的measure方法传入的模式是EXACTLY,因为子view会占据剩余容器的空间,所以它大小是确定的。 
      而当设置为 wrap_content时,容器传进去的是AT_MOST, 表示子view的大小最多是多少,这样子view会根据这个上限来设置自己的尺寸。当子view的大小设置为精确值时,容器传入的是EXACTLY, 而MeasureSpec的UNSPECIFIED模式表示你没有指定大小。
    3. View的onMeasure方法默认行为是当模式为UNSPECIFIED时,设置尺寸为mMinWidth(通常为0)或者背景drawable的最小尺寸,当模式为EXACTLY或者AT_MOST时,尺寸设置为传入的MeasureSpec的大小。  
      有个观念需要纠正的是,fill_parent应该是子view会占据剩下容器的空间,而不会覆盖前面已布局好的其他view空间,当然后面布局子 view就没有空间给分配了,所以fill_parent属性对布局顺序很重要。以前所想的是把所有容器的空间都占满了,难怪google在2.2版本里 把fill_parent的名字改为match_parent. 
      在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。 
      接下来的框架代码给出了处理View测量的典型实现:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec); setMeasuredDimension(measuredHeight, measuredWidth); // 记住这句可不能省。
} private int measureHeight(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec); // Default size if no limits are specified.
int result = 500; if (specMode == MeasureSpec.AT_MOST) {
// Calculate the ideal size of your
// control within this maximum size.
// If your control fills the available
// space return the outer bound.
result = specSize;
} else if (specMode == MeasureSpec.EXACTLY) {
// If your control can fit within these bounds return that value.
result = specSize;
}
return result;
} private int measureWidth(int measureSpec) {
// 代码基本类似measureHeight
}

总结:

通过

            int specMode = MeasureSpec.getMode(measureSpec); 
            int specSize = MeasureSpec.getSize(measureSpec);

这2个值,然后计算自己想要占有的宽和高。

2.onDraw

这个不解释了。(后期会细说canvas和paint)

最新文章

  1. SQL操作记录查看工具
  2. iOSReachability判断网络连接状态
  3. hibernate--lazy(懒加载)属性
  4. yii之srbac详解
  5. 删CentOS / RHEL库和配置文件(Repositories and configuraiton files)
  6. C#继承关系中【方发表】的创建和调用
  7. C#使用Gecko实现浏览器
  8. Hibernate 相关配置
  9. leetcode — set-matrix-zeroes
  10. Win10提示无法创建新的分区也找不到现有的分区解法
  11. js监听浏览器tab窗口切换
  12. windows上apache是线程处理请求,linux上apache是进程处理请求
  13. 详解python2 和 python3的区别-乾颐堂
  14. LeetCode517. Super Washing Machines
  15. JSP动作指令
  16. vim编程技巧
  17. HDU5875 Function
  18. java笔记之方法
  19. Xampp+Openfire+Spark的简单使用
  20. linux基础——关于chmod用户权限和文件的相关操作

热门文章

  1. LINE_NO in format of XXXX example -&gt; Line 10 is 0010
  2. Android零基础入门第89节:Fragment回退栈及弹出方法
  3. 用C实现OOP面向对象编程(1)
  4. Spring与IoC
  5. 数组Array
  6. HTML连载14-文字属性补充&amp;简写
  7. Keepalived双主模式配置流程
  8. Python自学day-15
  9. Mysql常用的查询语句,记录一下,好东西大家共享
  10. Spring中AOP相关源码解析