Android自定义ViewGroup:onMeasure与onLayout(1)

Android自定义ViewGroup:onMeasure与onLayout(1)

Android自定义一个ViewGroup,需要重写ViewGrouo里面的两个最重要的回调函数onMeasure()与onLayout()。如果开发者自己摆脱Android为我们做好的几套布局(如常见的线1性布局、相对布局、帧布局等等),往底层实现view呈现,那么我们就得在ViewGroup中小心计算和指定各个子view的位置和坐标。具体的,就是在onMeasure与onLayout里面,先计算出子view空间占据的位置,然后在onLayout里面layout()各个子view。
过程:
(1)写一个自定义的类继承自ViewGroup。
(2)在onMeasure()计算出ViewGroup占据的空间位置,其实就是宽和高。
(3)接着在onLayout()里面,在上一步计算框出的空间范围内一个一个的摆放layout子view。
本文以一个简化的例子说明,该例子实现常见的水平的线性布局,注意!本例出于简单演示的目的,仅仅实现一个水平的线性布局,如果是实现垂直的线性布局,那么细节地方的代码处理会不同,主要体现在测量高度和宽度及摆放的方向上。
写一个类MyLayout继承自ViewGroup,这个类名叫MyLayout还是MyViewGroup随意:

1package zhangphil.layout; 2 3import android.content.Context; 4import android.util.AttributeSet; 5import android.view.View; 6import android.view.ViewGroup; 7 8public class MyLayout extends ViewGroup { 9 10 public MyLayout(Context context, AttributeSet attrs) { 11 super(context, attrs); 12 } 13 14 // 度量全部子view要占用的空间,宽和高 15 //onMeasure被Android系统调用是在onLayout之前 16 @Override 17 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 18 19 //所有子view加起来总的Measured Dimension高度和宽度 20 int measuredWidth = 0; 21 int measuredHeight = 0; 22 23 int count = getChildCount(); 24 for (int i = 0; i < count; i++) { 25 View v = getChildAt(i); 26 if (v.getVisibility() != View.GONE) { 27 measureChild(v, widthMeasureSpec, heightMeasureSpec); 28 29 measuredWidth += v.getMeasuredWidth(); 30 31 //measuredDimensionHeight += v.getMeasuredHeight(); 32 measuredHeight=Math.max(measuredHeight, v.getMeasuredHeight()); 33 } 34 } 35 36 //仔细检查!不要疏忽掉一些padding的值 37 measuredWidth += getPaddingLeft() + getPaddingRight(); 38 measuredHeight += getPaddingTop() + getPaddingBottom(); 39 40 //可选 41 //measuredWidth = Math.max(measuredWidth, getSuggestedMinimumWidth()); 42 //measuredHeight = Math.max(measuredHeight, getSuggestedMinimumHeight()); 43 44 //另外一种set度量值的方法 45 //setMeasuredDimension(resolveSize(measuredWidth, widthMeasureSpec),resolveSize(measuredHeight, heightMeasureSpec)); 46 47 setMeasuredDimension(measuredWidth, measuredHeight); 48 } 49 50 //Android系统在onMeasure之后调用onLayout 51 @Override 52 protected void onLayout(boolean changed, int l, int t, int r, int b) { 53 //此时回调的参数l,t,r,b是上一步onMeasure计算出来的值。r是总宽度,b是总高度 54 //我们在l,t,r,b这四个参数“框”出来的空间内一个一个摆放我们自己的子view 55 56 int count = getChildCount(); 57 for (int i = 0; i < count; i++) { 58 View v = getChildAt(i); 59 if (v.getVisibility() != View.GONE) { 60 int childWidth = v.getMeasuredWidth(); 61 int childHeight = v.getMeasuredHeight(); 62 63 //开始摆放 64 v.layout(l, t, l + childWidth, t + childHeight); 65 66 //把左边的锚定位置往右移 67 l += childWidth; 68 69 //本例简单演示的是水平摆放子view,所以此处不用累加高度 70 //t += childHeight; 71 } 72 } 73 } 74}

写一个布局文件,这个布局不再使用Android已经为我们准备好的布局,而是MyLayout布局,MyLayout布局里面简单套几个TextView作为子布局:

1<?xml version="1.0" encoding="utf-8"?> 2<zhangphil.layout.MyLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <TextView 7 android:layout_width="100dip" 8 android:layout_height="50dip" 9 android:background="@android:color/holo_red_light" 10 android:gravity="center" 11 android:text="Zhang" /> 12 13 <TextView 14 android:layout_width="50dip" 15 android:layout_height="50dip" 16 android:background="@android:color/holo_blue_light" 17 android:gravity="center" 18 android:text="Phil" /> 19 20 <TextView 21 android:layout_width="80dip" 22 android:layout_height="50dip" 23 android:background="@android:color/holo_green_light" 24 android:gravity="center" 25 android:text="\@CSDN" /> 26</zhangphil.layout.MyLayout>

如果不想写xml布局也可以,那么在setContentView里面new一个MyLayout()直接放进去也一样。

写一个MainActivity.java测试,其实就是把xml布局直接setContentView()里面,除此之外没有其他任何代码:

1package zhangphil.layout; 2 3import android.app.Activity; 4import android.os.Bundle; 5 6public class MainActivity extends Activity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 } 13}

代码运行结果:

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条

问题1、页面布局文件:<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/layoutorderdetail"android:layoutwidth"matchparent"android:layoutheigh

Android-自定义view

要自定义view,都知道有3个方法需要重写:onMeasure、onLayout、onDraw。而且这三个方法的执行是按顺序的。生命周期image.png实际开发中,比较多的自定义都是具体实现一个view的子类,实现viewgroup的子类比较少,两者基本相似,区别就是view需要实现onMeasure、onLayout、onDraw三个方法,而vie

50 Android Hack 读书笔记

1、可以指定android:weightSum属性2、使用include标签来应对变化3、使用ViewStub标签延迟加载有可能不需要加载的数据标签中可以指定inflateId属性4、使用自定义ViewGroup,重写onMeasure、onLayout5、使用Android的PreferenceCategory6、使用TextSwitcher