/**     * onMeasure方法的作用时测量空间的大小     * @param widthMeasureSpec     * @param heightMeasureSpec     */    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        //设定默认值,当属性为wrap_comtent时使用,使用match_parent时是精确模式        int defaultWidthSpecSize = 200;        int defaultHeightSpecSize = 200;        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);/** *      Mode取值: *      UNSPECIFIED:未指定模式,View想多大就多大,父容器不做限制,一般用于系统内部的测量。 *      AT_MOST:最大模式,对应于wrap_comtent属性,只要尺寸不超过父控件允许的最大尺寸就行。 *      EXACTLY:精确模式,对应于match_parent属性和具体的数值,父容器测量出View所需要的大小,也就是specSize的值。 */        if (widthSpecMode == MeasureSpec.EXACTLY) {            defaultWidthSpecSize = MeasureSpec.getSize(widthMeasureSpec);        }        if (heightSpecMode == MeasureSpec.EXACTLY) {            defaultHeightSpecSize = MeasureSpec.getSize(heightMeasureSpec);        }        setMeasuredDimension(defaultWidthSpecSize, defaultHeightSpecSize);    }


自定义属性:

attrs.xml文件

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

在自定义View的构造函数中获取

    public CustomView(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);        mColor = ta.getColor(R.styleable.CustomView_myColor, Color.GREEN);        //获取完成后及时释放资源        ta.recycle();        init();    }



更多相关文章

  1. Android系统配置数据库注释(settings.db)
  2. Android开发——Android搜索框架(二)
  3. [Android] ACTION_GET_CONTENT与ACTION_PICK的区别
  4. Android(安卓)UI系列 - 布局 - 目录
  5. Android——Activity四种启动模式
  6. android Manifest.xml选项-android:ConfigChanges
  7. linearLayout 和 relativeLayout的属性区别
  8. Android(安卓)属性动画(Property Animation) 完全解析 (下)
  9. Android修改字体样式

随机推荐

  1. EditText 里的属性
  2. Android 5.0特性(SwipeRefreshLayout)
  3. 利用ContentProvider实现同步Binder
  4. android浏览器研究-下载
  5. Android(安卓)屏幕保护程序制作及源码
  6. Ubuntu 8.04下编译Android源码全过程
  7. Android Selector原理
  8. Android中的停止状态
  9. 百度地图SDK for Android v2.1.3全新发布
  10. android layout 按比例布局