Android 自定义组件之如何实现自定义组件
参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699
简介
Android提供了用于构建UI的强大的组件模型。两个基类:View和ViewGroup。
可用Widget的部分名单包括Button, TextView, EditText, ListView, CheckBox,RadioButton, Gallery, Spinner,以及一些有特别作用的组件: AutoCompleteTextView, ImageSwitcher和 TextSwitcher。
可用的布局有:LinearLayout,FrameLayout,RelativeLayout,AbsoluteLayout,GridLayout (later on api level 14 or v7-support)
基本做法
1. 继承自View或View的子类
2. 重写父类的一些方法,如:onDraw(),onMeasure(),onLayout()等
3. 使用自定义的组件类。
1. 最普通的作法是,继承自View,实现你的自定义组件
2. 提供一个构造函数,采用有属性参数的,也可以使用自定义属性
3. 你可能想在组件中创建自己的事件监听器,属性访问器和修改器,或其他行为
4. 几乎肯定要重写onDraw(),onMeasure()。默认onDraw()什么也没作,onMeasure()则设置一个100x100的尺寸。
5. 根据需要重写其他方法 ...
onDraw()和onMeasure()
onDraw(),提供一个Canvas,可以绘制2D图形。
若要绘制3D图形,请继承GLSurfaceView,参见,api-demo下的 GLSurfaceViewActivity
onMeasure() 测量组件
1. 宽度和高度在需要测量时调用该方法
2. 应该进行测量计算组件将需要呈现的宽度和高度。它应该尽量保持传入的规格范围内,尽管它可以选择超过它们(在这种情况下,父视图可以选择做什么,包括裁剪,滚动,抛出一个异常,或者要求onMeasure()再次尝试,或使用不同的测量规格)
3. 宽高计算完毕后,必须调用用setMeasuredDimession(int width, int height),进行设置。否则将抛出一个异常
下面是一些View中可被调用的方法总结(未全部包含,可自行查看类似onXxx的方法):
Category | Methods | Description |
---|---|---|
Creation | Constructors |
There is a form of the constructor that are called when the view is created from code and a form that is called when the view is inflated from a layout file. The second form should parse and apply any attributes defined in the layout file. |
|
Called after a view and all of its children has been inflated from XML. | |
Layout |
|
Called to determine the size requirements for this view and all of its children. |
|
Called when this view should assign a size and position to all of its children. | |
|
Called when the size of this view has changed. | |
Drawing |
|
Called when the view should render its content. |
Event processing |
|
Called when a new key event occurs. |
|
Called when a key up event occurs. | |
|
Called when a trackball motion event occurs. | |
|
Called when a touch screen motion event occurs. | |
Focus |
|
Called when the view gains or loses focus. |
|
Called when the window containing the view gains or loses focus. | |
Attaching |
|
Called when the view is attached to a window. |
|
Called when the view is detached from its window. | |
|
Called when the visibility of the window containing the view has changed. |
自定义View示例
adi-demo下的示例:LabelView
- /*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package android.widget;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.view.View;
- /**
- * Example of how to write a custom subclass of View. LabelView
- * is used to draw simple text views. Note that it does not handle
- * styled text or right-to-left writing systems.
- *
- */
- public class LabelView extends View {
- /**
- * Constructor. This version is only needed if you will be instantiating
- * the object manually (not from a layout XML file).
- * @param context the application environment
- */
- public LabelView(Context context) {
- super(context);
- initLabelView();
- }
- /**
- * Construct object, initializing with any attributes we understand from a
- * layout file. These attributes are defined in
- * SDK/assets/res/any/classes.xml.
- *
- * @see android.view.View#View(android.content.Context, android.util.AttributeSet)
- public LabelView(Context context, AttributeSet attrs) {
- super(context, attrs);
- initLabelView();
- Resources.StyledAttributes a = context.obtainStyledAttributes(attrs,
- R.styleable.LabelView);
- CharSequence s = a.getString(R.styleable.LabelView_text);
- if (s != null) {
- setText(s.toString());
- }
- ColorStateList textColor = a.getColorList(R.styleable.
- LabelView_textColor);
- if (textColor != null) {
- setTextColor(textColor.getDefaultColor(0));
- }
- int textSize = a.getInt(R.styleable.LabelView_textSize, 0);
- if (textSize > 0) {
- setTextSize(textSize);
- }
- a.recycle();
- }
- */
- private void initLabelView() {
- mTextPaint = new Paint();
- mTextPaint.setAntiAlias(true);
- mTextPaint.setTextSize(16);
- mTextPaint.setColor(0xFF000000);
- mPaddingLeft = 3;
- mPaddingTop = 3;
- mPaddingRight = 3;
- mPaddingBottom = 3;
- }
- /**
- * Sets the text to display in this label
- * @param text The text to display. This will be drawn as one line.
- */
- public void setText(String text) {
- mText = text;
- requestLayout();
- invalidate();
- }
- /**
- * Sets the text size for this label
- * @param size Font size
- */
- public void setTextSize(int size) {
- mTextPaint.setTextSize(size);
- requestLayout();
- invalidate();
- }
- /**
- * Sets the text color for this label
- * @param color ARGB value for the text
- */
- public void setTextColor(int color) {
- mTextPaint.setColor(color);
- invalidate();
- }
- /**
- * @see android.view.View#measure(int, int)
- */
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- setMeasuredDimension(measureWidth(widthMeasureSpec),
- measureHeight(heightMeasureSpec));
- }
- /**
- * Determines the width of this view
- * @param measureSpec A measureSpec packed into an int
- * @return The width of the view, honoring constraints from measureSpec
- */
- private int measureWidth(int measureSpec) {
- int result;
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
- if (specMode == MeasureSpec.EXACTLY) {
- // We were told how big to be
- result = specSize;
- } else {
- // Measure the text
- result = (int) mTextPaint.measureText(mText) + mPaddingLeft
- + mPaddingRight;
- if (specMode == MeasureSpec.AT_MOST) {
- // Respect AT_MOST value if that was what is called for by measureSpec
- result = Math.min(result, specSize);
- }
- }
- return result;
- }
- /**
- * Determines the height of this view
- * @param measureSpec A measureSpec packed into an int
- * @return The height of the view, honoring constraints from measureSpec
- */
- private int measureHeight(int measureSpec) {
- int result;
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
- mAscent = (int) mTextPaint.ascent();
- if (specMode == MeasureSpec.EXACTLY) {
- // We were told how big to be
- result = specSize;
- } else {
- // Measure the text (beware: ascent is a negative number)
- result = (int) (-mAscent + mTextPaint.descent()) + mPaddingTop
- + mPaddingBottom;
- if (specMode == MeasureSpec.AT_MOST) {
- // Respect AT_MOST value if that was what is called for by measureSpec
- result = Math.min(result, specSize);
- }
- }
- return result;
- }
- /**
- * Render the text
- *
- * @see android.view.View#onDraw(android.graphics.Canvas)
- */
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.drawText(mText, mPaddingLeft, mPaddingTop - mAscent, mTextPaint);
- }
- private Paint mTextPaint;
- private String mText;
- private int mAscent;
- }
应用该自定义组件的layout xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <com.example.android.apis.view.LabelView
- android:background="@drawable/red"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:text="Red"/>
- <com.example.android.apis.view.LabelView
- android:background="@drawable/blue"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:text="Blue" app:textSize="20dp"/>
- <com.example.android.apis.view.LabelView
- android:background="@drawable/green"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:text="Green" app:textColor="#ffffffff" />
- </LinearLayout>
该示例演示了:
1. 继承自View的完全自定义组件
2. 带参数的构造函数(一些属性参数在xml中设置)。还使用了自定义属性 R.styleable.LabelView
3. 一些标准的public 方法,如setText()、setTextSize()、setTextColor()
4. onMeasure()测量组件尺寸,内部由measureWidth(int measureSpec) 和 measureHeight(int measureSpec)来测量。
5. onDraw()将Label绘制到画面Canvas上
复合组件
复合组件示例
- private class SpeechView extends LinearLayout {
- public SpeechView(Context context, String title, String dialogue, boolean expanded) {
- super(context);
- this.setOrientation(VERTICAL);
- // Here we build the child views in code. They could also have
- // been specified in an XML file.
- mTitle = new TextView(context);
- mTitle.setText(title);
- addView(mTitle, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
- mDialogue = new TextView(context);
- mDialogue.setText(dialogue);
- addView(mDialogue, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
- mDialogue.setVisibility(expanded ? VISIBLE : GONE);
- }
- /**
- * Convenience method to set the title of a SpeechView
- */
- public void setTitle(String title) {
- mTitle.setText(title);
- }
- /**
- * Convenience method to set the dialogue of a SpeechView
- */
- public void setDialogue(String words) {
- mDialogue.setText(words);
- }
- /**
- * Convenience method to expand or hide the dialogue
- */
- public void setExpanded(boolean expanded) {//该方法在List4中没有
- mDialogue.setVisibility(expanded ? VISIBLE : GONE);
- }
- private TextView mTitle;
- private TextView mDialogue;
- }
SpeachView,继承了LinearLayout,纵向布局。内部有一个TextView的title,一个TextView的dialogue。List4完全展开两个TextView;List6点击title可以收缩/展开dialogue。
修改现有View类型
- public static class LinedEditText extends EditText {
- private Rect mRect;
- private Paint mPaint;
- // This constructor is used by LayoutInflater
- public LinedEditText(Context context, AttributeSet attrs) {
- super(context, attrs);
- // Creates a Rect and a Paint object, and sets the style and color of the Paint object.
- mRect = new Rect();
- mPaint = new Paint();
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setColor(0x800000FF);
- }
- /**
- * This is called to draw the LinedEditText object
- * @param canvas The canvas on which the background is drawn.
- */
- @Override
- protected void onDraw(Canvas canvas) {
- // Gets the number of lines of text in the View.
- int count = getLineCount(); //edittext中有几行, edittext继承textview
- // Gets the global Rect and Paint objects
- Rect r = mRect;
- Paint paint = mPaint;
- /*
- * Draws one line in the rectangle for every line of text in the EditText
- */
- for (int i = 0; i < count; i++) {
- // Gets the baseline coordinates for the current line of text
- int baseline = getLineBounds(i, r);//将一行的范围坐标赋给矩形r;返回一行y方向上的基准线坐标
- /*
- * Draws a line in the background from the left of the rectangle to the right,
- * at a vertical position one dip below the baseline, using the "paint" object
- * for details.
- */
- canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);//绘制一条线,宽度为原行的宽度,高度为从基线开始+1个像素
- }
- // Finishes up by calling the parent method
- super.onDraw(canvas);
- }
- }
定义
类的初始化
重写的方法
使用自定义组件
- <view xmlns:android="http://schemas.android.com/apk/res/android"
- class="com.example.android.notepad.NoteEditor$LinedEditText"
- android:id="@+id/note"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@android:color/transparent"
- android:padding="5dp"
- android:scrollbars="vertical"
- android:fadingEdge="vertical"
- android:gravity="top"
- android:textSize="22sp"
- android:capitalize="sentences"
- />
使用完全限定类名,引入自定义组件。使用$引用内部类。
Android 自定义组件之如何实现自定义组件的更多相关文章
- vue_组件间通信:自定义事件、消息发布与订阅、槽
自定义事件 只能用于 子组件 向 父组件 发送数据 可以取代函数类型的 props 在父组件: 给子组件@add-todo-event="addTodo" 在子组件: 相关方法中, ...
- 微信小程序之自定义select下拉选项框组件
知识点:组件,animation,获取当前点击元素的索引与内容 微信小程序中没有select下拉选项框,所以只有自定义.自定义的话,可以选择模板的方式,也可以选择组件的方式来创建. 这次我选择了组件, ...
- 微信小程序自定义组件封装及父子间组件传值
首先在我们可以直接写到需要的 page 中,然后再进行抽取组件,自定义组件建议 wxzx-xxx 命名 官网地址:https://developers.weixin.qq.com/miniprogra ...
- 微信小程序 - 自定义swiper dots样式(非组件)
自定义须知: :组件内无法使用自定义样式变化,需要自定义 :原理就是利用swiper change事件与下标index匹配,显示不同的样式 swiper组件须知: :一旦swiper用于组件化,就 ...
- Vue基础-自定义事件的表单输入组件、自定义组件的 v-model
Vue 测试版本:Vue.js v2.5.13 学习 Vue 的自定义事件的表单输入组件,觉得文档讲的不太细致,所以这里再细化一下: 如果不用 v-model,代码应该是这样: <myinput ...
- Wuss Weapp 一款高质量,组件齐全,高自定义的微信小程序 UI 组件库
Wuss Weapp 一款高质量,组件齐全,高自定义的微信小程序 UI 组件库 文档 https://phonycode.github.io/wuss-weapp 扫码体验 使用微信扫一扫体验小程序组 ...
- vue2.0 自定义 侧滑删除(LeftSlider)组件
1.自定义侧滑删除组件 LeftSlider.vue <!-- 侧滑删除 组件 --> <template> <div class="delete"& ...
- 微信小程序 自定义组件(modal) 引入组件
项目结构: 步骤一:创建组件 声明这一组文件为自定义组件 modal.json { "component": true, // 自定义组件声明 "usingCompone ...
- DRF框架(九)——drf偏移分页组件、drf游标分页组件(了解)、自定义过滤器、过滤器插件django-filter
drf偏移分页组件 paginations.py from rest_framework.pagination import LimitOffsetPagination class MyLimitOf ...
随机推荐
- Python-Cpython解释器支持的进程与线程
一.Python并发编程之多进程 1. multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在pyt ...
- 解决hash冲突的办法
1.开发定址法 2.再哈希法 3.链地址法 4.建立一个公共溢出区
- 第四课 Makefile文件的制作(下)
1序言: 前面一节课讲解了Makefile的基础知识包括原理.预定义以及命令格式,这样是可以完成一个自动编译的文件,这些知识可以帮你完成.想想mak真是强大啊,可能有些同志发现了如果项目文件太多每个目 ...
- HDU - 6406 Taotao Picks Apples (RMQ+dp+二分)
题意:N个高度为hi的果子,摘果子的个数是从位置1开始从左到右的严格递增子序列的个数.有M次操作,每次操作对初始序列修改位置p的果子高度为q.每次操作后输出修改后能摘到得数目. 分析:将序列分为左.右 ...
- LightOJ - 1336 Sigma Function(约数和+整数拆分)
题干中给出函数公式: 其中pi为n的每个素因数,ei为其个数.设该函数为F(x),其意义为x的约数之和.问在1-n中有多少x,令F(x)为偶数. 分析:设f(p)为(p^(e+1)-1)/(p-1). ...
- SSDB系列文章推荐
1. 下载和安装: http://ssdb.io/docs/zh_cn/install.html 2. SSDB 文档 http://ssdb.io/docs/zh_cn/index.html ...
- 一个linux命令(6/13):traceroute命令
通过traceroute 我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不 ...
- os包方法
os包中实现了平台无关的接口,设计向Unix风格,但是错误处理是go风格,当os包使用时,如果失败之后返回错误类型而不是错误数量. os包中函数设计方式和Unix类似,下面来看一下. func Chd ...
- 【bzoj3170】[Tjoi2013]松鼠聚会(数学题)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3170 这道题要在n个点中求一个点使其他点到该点的切比雪夫距离最小. 有个结论:$ (x ...
- git 分支管理 (转自廖雪峰的git教程)
在版本回退里,你已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master分支.HEAD严格来说不是指向提交,而 ...