效果图

布局文件

<?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="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#003839"
android:gravity="center"
android:visibility="visible"
android:text="SECOND"/> <com.pengkv.apple.weight.FirstView
android:layout_width="wrap_content"
android:visibility="visible"
android:background="#888787"
android:layout_height="wrap_content"/> </LinearLayout>

View代码

public class FirstView extends LinearLayout {

    public FirstView(Context context) {//代码实例化的时候调用
super(context);
} public FirstView(Context context, AttributeSet attrs) {//布局文件引用的时候调用
super(context, attrs);
} public FirstView(Context context, AttributeSet attrs, int defStyleAttr) {//自己定义属性值的时候调用
super(context, attrs, defStyleAttr);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec,heightMeasureSpec);
setMeasuredDimension(measureSpec(widthMeasureSpec),measureSpec(heightMeasureSpec)); //重測尺寸
} public int measureSpec(int measureSpec){
int result=0;
int specMode=MeasureSpec.getMode(measureSpec);//获取測量模式
int specSize=MeasureSpec.getSize(measureSpec);//获取測量尺寸 if (specMode==MeasureSpec.EXACTLY){//精确模式:包含准确设置dp值和match_parent
result=specSize;
}else {
result=400;//默认设置的尺寸
if (specMode==MeasureSpec.AT_MOST){
result=Math.min(result,specSize);
}
}
return result;
}
}

自己定义View-2-重写onMeasure的更多相关文章

  1. Android 自己定义View须要重写ondraw()等方法

    Android  自己定义View须要重写ondraw()等方法.这篇博客给大家说说自己定义View的写法,须要我们继承View,然后重写一些 方法,方法多多,看你须要什么方法 首先写一个自己定义的V ...

  2. 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件

    Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...

  3. Android 它们的定义View (一)

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 非常Android入门程序员AndroidView.可能都是比較恐 ...

  4. Android 它们的定义View它BounceProgressBar

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/41246247 [Rocko's blog] 之前几天下载了非常久没用了的桌面版酷狗来用用的时候 ...

  5. Android 自己定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...

  6. 【android自己定义控件】自己定义View属性

    1.自己定义View的属性 2.在View的构造方法中获得我们自己定义的属性 3.重写onMesure 4.重写onDraw 3这个步骤不是必须,当然了大部分情况下还是须要重写的. 1.自己定义Vie ...

  7. ANDROID定义自己的看法——onMeasure,MeasureSpec源代码 过程 思考具体解释

    一个简短的引论: 在他们的定义view什么时候,其实很easy,只需要知道3: 1.測量--onMeasure():决定View的大小 2.布局--onLayout():决定View在ViewGrou ...

  8. 自己定义View之绘制圆环

    一.RingView 自己定义的view,构造器必须重写,至于重写哪个方法,參考例如以下: ①假设须要改变View绘制的图像,那么须要重写OnDraw方法.(这也是最经常使用的重写方式.) ②假设须要 ...

  9. 图文剖析自己定义View的绘制(以自己定义滑动button为例)

    自己定义View一直是横在Android开发人员面前的一道坎. 一.View和ViewGroup的关系 从View和ViewGroup的关系来看.ViewGroup继承View. View的子类.多是 ...

  10. 自己定义View Layout过程 - 最易懂的自己定义View原理系列(3)

    前言 自己定义View是Android开发人员必须了解的基础 网上有大量关于自己定义View原理的文章.但存在一些问题:内容不全.思路不清晰.无源代码分析.简单问题复杂化等等 今天,我将全面总结自己定 ...

随机推荐

  1. ORA-14404: partitioned table contains partitions in a different tablespace

    SQL> drop tablespace nn_data including contents and datafiles; drop tablespace nn_data including ...

  2. 绿色版mysql注册卸载服务

    如果直接用绿色版的mysql,则下载后解压,只需对目录下的my.ini文件的basedir(mysql的基本目录)和datadir(mysql数据目录)指定就可以,如下所示. #Path to ins ...

  3. Android中使用IntentService运行后台任务

    IntentService提供了一种在后台线程中运行任务的方式,适合处理运行时间较长的后台任务. 长处: (1)IntentService执行在单独的线程中.不会堵塞UI线程 (2)IntentSer ...

  4. 《从零開始学Swift》学习笔记(Day 65)——Cocoa Touch设计模式及应用之选择器

    原创文章,欢迎转载.转载请注明:关东升的博客 实现目标与动作关联使用UIControl类addTarget(_:action:forControlEvents:)方法,演示样例代码例如以下: butt ...

  5. ||在oracle数据库中起到字符串拼接的作用

    例子:select org.id from org where inner_code like '12011601001' || '%' ||在oracle数据库中起到字符串拼接的作用,上面等同于'1 ...

  6. 使用 sqlyog 导入导出数据显示 lost connection to mysql server during query

    mysql中经常需要备份数据,在使用 sqlyog 进行备份数据库为转储文件,然后在其他数据库中导入发生 lost connection 经过查询大量资料是数据库配置的 max_allowed_pac ...

  7. python yield的解释

    链接地址: http://pyzh.readthedocs.io/en/latest/the-python-yield-keyword-explained.html https://www.jians ...

  8. 如何在Linux平台下安装JDK

    原文链接:http://android.eoe.cn/topic/android_sdk 1.下载Java的开发包JDK: JDK有好几个类型版本,我们只需要选择Java SE类型的版本就行了. 进入 ...

  9. 【Unity】5.1 3D坐标系基础知识

    分类:Unity.C#.VS2015 创建日期:2016-04-20 一.简介 在虚拟的游戏世界中,与3D有关的数学知识决定了游戏引擎如何计算和模拟出开发者以及玩家看到的每一帧画面.学习或者回想一下基 ...

  10. Linux 索引节点(inode)详解

    参考文章:http://www.ruanyifeng.com/blog/2011/12/inode.html