1、自己定义View的属性

2、在View的构造方法中获得我们自己定义的属性

3、重写onMesure

4、重写onDraw

3这个步骤不是必须,当然了大部分情况下还是须要重写的。

1、自己定义View的属性,首先在res/values/  下建立一个attrs.xml , 在里面定义我们的属性和声明我们的整个样式。

<?xml version="1.0" encoding="utf-8"?>
<resources> <attr name="txtName" format="string"/>
<attr name="txtColor" format="color"/>
<attr name="txtSize" format="dimension" /> <declare-styleable name="titleStyle">
<attr name="txtName"/>
<attr name="txtColor"/>
<attr name="txtSize"/>
</declare-styleable>
</resources>

定义了字体,字体颜色,字体大小3个属性,format是值该属性的取值类型:

一共同拥有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;

编写的时候工具会提醒你使用哪种,不知道也能够Google搜索下

接下来就自己定义View

public class CustomTitleView extends View{

    private  String txtName;
private int txtColor,txtSize;
private Paint mPaint;
private Rect mBounds; public CustomTitleView(Context context) {
this(context, null);
} public CustomTitleView(Context context, AttributeSet attrs) {
this(context, attrs, 0); } public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
//详细操作
}
}

定义完自己定义的View ,就该调用我们自己定义的View了。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:title="http://schemas.android.com/apk/res/com.example.androidDemo" 《》
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <com.example.androidDemo.View.CustomTitleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="5dp"
title:txtName="你好"
title:txtColor="#ffffff"
title:txtSize="16sp"/>
</RelativeLayout>

注意代码中的这行,自己定义命名空间,com.example.androidDemo是项目包路径

xmlns:title="http://schemas.android.com/apk/res/com.example.androidDemo"

使用自己定义命名空间:

        title:txtName="你好"
title:txtColor="#ffffff"
title:txtSize="16sp"

在View的构造方法中,获得我们的自己定义的样式

public class CustomTitleView extends View{

    private  String txtName;
private int txtColor,txtSize;
private Paint mPaint;
private Rect mBounds; public CustomTitleView(Context context) {
this(context, null);
} public CustomTitleView(Context context, AttributeSet attrs) {
this(context, attrs, 0); } public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,R.styleable.titleStyle,defStyleAttr,0); int n = typedArray.getIndexCount(); for (int i = 0; i < n; i++){
int attr = typedArray.getIndex(i);
switch (attr){ case 0: txtName = typedArray.getString(attr);
break;
case 1: txtColor = typedArray.getColor(attr, Color.BLACK);
break;
case 2:
txtSize = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
break; }
}
typedArray.recycle(); /**
* 获得绘制文本的宽和高
*/
mPaint = new Paint();
mPaint.setTextSize(txtSize);
// mPaint.setColor(mTitleTextColor);
mBounds = new Rect();
mPaint.getTextBounds(txtName, 0, txtName.length(), mBounds);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height ;
if (widthMode == MeasureSpec.EXACTLY)
{
width = widthSize;
} else
{
mPaint.setTextSize(txtSize);
mPaint.getTextBounds(txtName, 0, txtName.length(), mBounds);
float textWidth = mBounds.width();
int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
width = desired;
} if (heightMode == MeasureSpec.EXACTLY)
{
height = heightSize;
} else
{
mPaint.setTextSize(txtSize);
mPaint.getTextBounds(txtName, 0, txtName.length(), mBounds);
float textHeight = mBounds.height();
int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
height = desired;
} setMeasuredDimension(width, height); } @Override
protected void onDraw(Canvas canvas) {
mPaint.setColor(Color.YELLOW);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint); mPaint.setColor(txtColor);
canvas.drawText(txtName, getWidth() / 2 - mBounds.width() / 2, getHeight() / 2 + mBounds.height() / 2, mPaint); }
}

当中

MeasureSpec.EXACTLY推断你传人的宽,高是不是精确赋值

android:layout_width="wrap_content"

 android:layout_height="wrap_content"

假设是wrap_content,

 mPaint.setTextSize(txtSize);
mPaint.getTextBounds(txtName, 0, txtName.length(), mBounds);
float textWidth = mBounds.width();
int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
width = desired;

假设是200dp这类精确的宽高值,

if (widthMode == MeasureSpec.EXACTLY)
{
width = widthSize;
}

效果图 ,是不是非常好用呢

【android自己定义控件】自己定义View属性的更多相关文章

  1. Android自己定义控件之应用程序首页轮播图

    如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...

  2. 【Android】自己定义控件实现可滑动的开关(switch)

    ~转载请注明来源:http://blog.csdn.net/u013015161/article/details/46704745 介绍 昨天晚上写了一个Android的滑动开关, 即SlideSwi ...

  3. Android自己定义控件系列案例【五】

    案例效果: 案例分析: 在开发银行相关client的时候或者开发在线支付相关client的时候常常要求用户绑定银行卡,当中银行卡号一般须要空格分隔显示.最常见的就是每4位数以空格进行分隔.以方便用户实 ...

  4. Android自定义控件1--自定义控件介绍

    Android控件基本介绍 Android本身提供了很多控件比如我们常用的有文本控件TextView和EditText:按钮控件Button和ImageButton状态开关按钮ToggleButton ...

  5. Android自己定义控件(状态提示图表)

    [工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重分享成果] 1 背景 前面分析那么多系统源代码了.也该暂停下来歇息一下,趁昨晚闲着看见一个有意思的需求就操 ...

  6. 【Android】自己定义控件——仿天猫Indicator

    今天来说说类似天猫的Banner中的小圆点是怎么做的(图中绿圈部分) 在学习自己定义控件之前,我用的是很二的方法,直接在布局中放入多个ImageView,然后代码中依据Pager切换来改变图片.这样的 ...

  7. android学习七(创建自己定义控件)

    前面学习的是android的基本控件和布局的使用,可是主要的控件和布局有时候并不能实现复杂的布局.我们来看下各种控件和布局的关系. 可见全部的控件都是直接或者间接的继承自View的,全部的布局都是直接 ...

  8. Android自己定义控件系列五:自己定义绚丽水波纹效果

    尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...

  9. Android自己定义控件系列二:自己定义开关button(一)

    这一次我们将会实现一个完整纯粹的自己定义控件,而不是像之前的组合控件一样.拿系统的控件来实现.计划分为三部分:自己定义控件的基本部分,自己定义控件的触摸事件的处理和自己定义控件的自己定义属性: 以下就 ...

  10. Android自己定义控件:进度条的四种实现方式

    前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...

随机推荐

  1. delphi之完美Splash方案(在TfrmMain.FormCreate里不断调用TfrmSplash显示加载进度文字,并且及时Update显示)

    前言:网上有很多介绍delphi创建闪屏的代码,大多只是在程序开启前显示一个闪屏,但是却没有说如何在闪屏上显示程序加载的进度,于是笔者有意思介绍一下这种闪屏方式. 1.创建一个窗体(TfrmSplas ...

  2. heap表和iot表排序规则不同

    heap 和iot 对比 OBJECT_NAME OBJECT_TYPE --------------------------------------------------------------- ...

  3. MFC 总体理解

    在MFC程序中,我们并不经常直接调用Windows API,而是从MFC类创建对象并调用属于这些对象的成员函数.也就是说MFC封装了Windows API 你说你喜欢C++而MFC换一种说法就是一个用 ...

  4. Firemonkey 自定义Button的Style

    这篇文章模仿HTML中基于CSS的Button,通过Style实现自定义样式的Button. 前言 主要模仿的CSS代码如下: CSS Code 123456789101112131415161718 ...

  5. 基于MMSeg算法的中文分词类库

    原文:基于MMSeg算法的中文分词类库 最近在实现基于lucene.net的搜索方案,涉及中文分词,找了很多,最终选择了MMSeg4j,但MMSeg4j只有Java版,在博客园上找到了*王员外*(ht ...

  6. jsp中将后台传递过来的json格式的list数据绑定到下拉菜单select

    <span style="white-space:pre"> </span> <select><c:forEach var="f ...

  7. uploadify,实际开发案例【选择完文件点击上传才上传】

    <script type="text/javascript"> )+Math.floor(Math.random()*)+']-'; //设置随机文件前缀. $k(fu ...

  8. java entry

    我希望要一个ArrayList<Entry>,类似C++中的pair, 可是Map.Entry是个接口,不能实例化,能够像以下这样写 HashMap<Integer, Integer ...

  9. QQ圈子降级为“应用”后应关注其隐私设置

    在之前的QQ版本中,QQ圈子的权限设置在“系统设置”对话框的“权限设置”中,如图所示. 但是在更新后的2013SP1版本中,“系统设置”对话框中的“权限设置”已经没有了“圈子权限” QQ圈子成了应用管 ...

  10. Unity3D-深入剖析NGUI的游戏UI架构

    Unity3D-NGUI分析,使用NGUI做UI须要注意的几个要点在此我想罗列一下,对我在U3D上做UI的一些总结,最后解剖一下NGUI的源码.它是假设架构和运作的. 在此前我介绍了自己项目的架构方式 ...