自己定义View-2-重写onMeasure
效果图
布局文件
<?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的更多相关文章
- Android 自己定义View须要重写ondraw()等方法
Android 自己定义View须要重写ondraw()等方法.这篇博客给大家说说自己定义View的写法,须要我们继承View,然后重写一些 方法,方法多多,看你须要什么方法 首先写一个自己定义的V ...
- 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件
Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...
- Android 它们的定义View (一)
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 非常Android入门程序员AndroidView.可能都是比較恐 ...
- Android 它们的定义View它BounceProgressBar
转载请注明出处:http://blog.csdn.net/bbld_/article/details/41246247 [Rocko's blog] 之前几天下载了非常久没用了的桌面版酷狗来用用的时候 ...
- Android 自己定义View (二) 进阶
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...
- 【android自己定义控件】自己定义View属性
1.自己定义View的属性 2.在View的构造方法中获得我们自己定义的属性 3.重写onMesure 4.重写onDraw 3这个步骤不是必须,当然了大部分情况下还是须要重写的. 1.自己定义Vie ...
- ANDROID定义自己的看法——onMeasure,MeasureSpec源代码 过程 思考具体解释
一个简短的引论: 在他们的定义view什么时候,其实很easy,只需要知道3: 1.測量--onMeasure():决定View的大小 2.布局--onLayout():决定View在ViewGrou ...
- 自己定义View之绘制圆环
一.RingView 自己定义的view,构造器必须重写,至于重写哪个方法,參考例如以下: ①假设须要改变View绘制的图像,那么须要重写OnDraw方法.(这也是最经常使用的重写方式.) ②假设须要 ...
- 图文剖析自己定义View的绘制(以自己定义滑动button为例)
自己定义View一直是横在Android开发人员面前的一道坎. 一.View和ViewGroup的关系 从View和ViewGroup的关系来看.ViewGroup继承View. View的子类.多是 ...
- 自己定义View Layout过程 - 最易懂的自己定义View原理系列(3)
前言 自己定义View是Android开发人员必须了解的基础 网上有大量关于自己定义View原理的文章.但存在一些问题:内容不全.思路不清晰.无源代码分析.简单问题复杂化等等 今天,我将全面总结自己定 ...
随机推荐
- 代码管理(五)git 删除分支
1.删除远程分支 在远程下面,选择需要删除的分支,右击,选择删除 2. 删除不存在远程对应分支的本地分支 在远程上建立了一个分支feature,后来leader觉得不合理,就把远程feature分支 ...
- shell value too great for base
今天同步文件的时候发现了这个问题: 原因是: tar_file_month=`date +%m` rsync -auzq --exclude=))"]" 当月是8月,tar_fil ...
- Easyui combobox onChange事件
Easyui combobox onChange事件: 注册事件: $(function () { $('#cc_id').combobox({ onChange: function (newValu ...
- STM8的数据@near @tiny定义
总是记不住这个:stm8 stvd下 near等于51的xdata tiny等于51的idata http://www.waveshare.net/article/STM8-3-1-10.htm 如何 ...
- 手把手教你ranorex_android源码instrument
话说ranorex能把android程序看的透彻,关键是在潜伏,他使用instrumentation,在每个界面(activity)里面,准确的说是onresume,也就是页面显示的时候,都给安装了个 ...
- Socket编程知识必学
端口号常识: 端口号被从1 开始分配. 通常端口号超出255 的部分被本地主机保留为私有用途. 1到255 之间的号码被用于远程应用程序所请求的进程和网络服务. 每个网络通信循 ...
- 上传下载 demo
import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.springf ...
- android辅助开发工具包介绍
辅助开发工具包(ADK)是为硬件制造商和业余爱好者准备的参考实现.硬件制造商和业余爱好者可以使用此工具包作为开发Android辅助设备的起点.每一个ADK发行版都将提供源代码和硬件规格,以使整个辅助设 ...
- wget常见用法
1.很多软件官网会有安装脚本,并把脚本搞成raw模式,方便下载后直接运行的shell文件.比如docker wget -qO- get.docker.com | bash -q的含义是:--quiet ...
- Atitit jquery 1.4--v1.11 v1.12 v2.0 3.0 的新特性
Atitit jquery 1.4--v1.11 v1.12 v2.0 3.0 的新特性 1.1. Jquery1.12 jQuery 2.2 和 1.12 新版本发布 - OPEN资讯.h ...