本篇文章主要介绍了"Android开发之自定义控件二---onLayout详解",主要涉及到方面的内容,对于Android开发感兴趣的同学可以参考一下:
话说一个乞丐在看一个程序员写程序,程序员遇到一个问题怎么都解决不了,这时乞丐说这少个分号,程序员照做结果问题解决了,就问:你怎么知道?乞丐笑笑说:我之前就是干这...
- package com.example.customviewpractice;
-
- import android.app.Activity;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.DisplayMetrics;
- import android.view.View;
- import android.view.ViewGroup;
-
- public class MyViewGroup extends ViewGroup {
-
- private Context mContext;
- private int sreenH;
-
- public MyViewGroup(Context context, AttributeSet attrs) {
- super(context, attrs);
- mContext = context;
- // 获取屏幕的高度
- sreenH = getScreenSize(((Activity) mContext))[1];
- }
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-
- super.onMeasure(widthMeasureSpec, widthMeasureSpec);
- // 测量子View
- measureChildren(widthMeasureSpec, heightMeasureSpec);
- }
-
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- // 获得子View个数
- int childCount = getChildCount();
- // 设置一个变量保存到父View左侧的距离
- int mLeft = 0;
- // 遍历子View
- for (int i = 0; i < childCount; i++) {
-
- View childView = getChildAt(i);
- // 获得子View的高度
- int childViewHeight = childView.getMeasuredHeight();
- // 获得子View的宽度
- int childViewWidth = childView.getMeasuredWidth();
- // 让子View在竖直方向上显示在屏幕的中间位置
- int height = sreenH / 2 - childViewHeight / 2;
- // 调用layout给每一个子View设定位置mLeft,mTop,mRight,mBottom.左上右下
- childView.layout(mLeft, height, mLeft + childViewWidth, height
- + childViewHeight);
- // 改变下一个子View到父View左侧的距离
- mLeft += childViewWidth;
- }
- }
-
- /**
- * 获取屏幕尺寸
- */
- public static int[] getScreenSize(Activity activity) {
- DisplayMetrics metrics = new DisplayMetrics();
- activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
- return new int[] { metrics.widthPixels, metrics.heightPixels };
- }
- }
布局文件[html] view
plain copy