Android自定义ViewGroup实现水平排列样式(1)

来源:互联网 发布:nginx服务器 编辑:程序博客网 时间:2024/06/06 16:33


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随意:
[java] view plain copy

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


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

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>
    <com. .viewgroup.MyLayout//此处包名要记得引用自己的
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="140dp"
            android:layout_height="100dp"
            android:background="#00FFFF"
            android:gravity="center"
            android:text="tang"
            android:textSize="25sp"

            android:textColor="@android:color/white"
            />

        <TextView
            android:layout_width="140dp"
            android:layout_height="100dp"
            android:background="#FFC0CB"
            android:gravity="center"
            android:text="jie"
            android:textSize="25sp"
            android:textColor="@android:color/white"
            />

        <TextView
            android:layout_width="140dp"
            android:layout_height="100dp"
            android:background="#AFEEEE"
            android:gravity="center"
            android:text="jie"
            android:textSize="25sp"
            android:textColor="@android:color/white"
            />
    </com. .viewgroup.MyLayout>

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



写一个MainActivity.java测试,其实就是把xml布局直接setContentView()里面,除此之外没有其他任何代码:
[html] view plain copy
  1.   
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4.   
  5. public class MainActivity extends Activity {  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.     }  
  12. }  

第二篇文章的链接,可自定义更多样式:http://blog.csdn.net/qingxuan521721/article/details/78753089


可参考郭霖大神的博客:http://blog.csdn.net/lmj623565791/article/details/38339817/

 


阅读全文
0 0
原创粉丝点击