自定义ViewGroup控件(一)----->流式布局进阶(一)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.SimpleLayout.MyLinLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff00ff"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#ff0000"
        android:text="第一个VIEW" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#00ff00"
        android:text="第二个VIEW" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="#0000ff"
        android:text="第三个VIEW" />

</com.example.SimpleLayout.MyLinLayout>

MainActivity

package com.example.SimpleLayout;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

MyLinLayout

package com.example.SimpleLayout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * onMeasure():测量自己的大小,自己的大小,为正式布局提供建议。(注意,只是建议,至于用不用,要看onLayout);
 * onLayout():使用layout()函数对所有子控件布局; onDraw():根据布局的位置绘图;
 * 
 */
public class MyLinLayout extends ViewGroup {
	/**
	 * 首先是3个构造器
	 */
	public MyLinLayout(Context context) {
		super(context);
	}

	public MyLinLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyLinLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	/**
	 * 此ViewGroup的宽高属性 android:layout_width="match_parent"--EXACTLY(确定)
	 * android:layout_height="wrap_content"--AT_MOST(不确定)
	 * 
	 * 他们是父类传递过来给当前view的一个建议值,建议值,即想把当前view的尺寸设置为宽widthMeasureSpec,
	 * 高heightMeasureSpec
	 * 
	 * ②、EXACTLY(完全),父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;
	 * ③、AT_MOST(至多),子元素至多达到指定大小的值。
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// 宽度、高度
		int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
		int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
		// 测量模式
		int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
		int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);
		// 初始化ViewGroup宽、高
		int viewGroupHeight = 0;
		int viewGroupWidth = 0;
		// 获取viewGroup中的每个孩子View,进行遍历
		int count = getChildCount();
		for (int i = 0; i < count; i++) {
			// 依次获取每个孩子View对象
			View child = getChildAt(i);
			// 测量每个孩子View,将父类的模式传进去--点开看源码
			measureChild(child, widthMeasureSpec, heightMeasureSpec);
			int childHeight = child.getMeasuredHeight();
			int childWidth = child.getMeasuredWidth();
			// ViewGroup高度递增
			viewGroupHeight += childHeight;
			// ViewGroup宽度取最大值
			viewGroupWidth = Math.max(childWidth, viewGroupWidth);
		}

		// ViewGroup的宽不需要测量直接"match_parent"--EXACTLY
		// 高是"wrap_content"--AT_MOST,需要累加得到高度
		/**
		 * ②、EXACTLY(完全),父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;
		 * ③、AT_MOST(至多),子元素至多达到指定大小的值。
		 */
		setMeasuredDimension(
				(measureWidthMode == MeasureSpec.EXACTLY) ? measureWidth
						: viewGroupWidth,
				(measureHeightMode == MeasureSpec.EXACTLY) ? measureHeight
						: viewGroupHeight);
	}

	/**
	 * getMeasureWidth()方法在measure()过程结束后就可以获取到了,而getWidth()方法要在layout()
	 * 过程结束后才能获取到。再重申一遍
	 */
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int top = 0;
		// 获取子View的数量
		int count = getChildCount();
		for (int i = 0; i < count; i++) {
			// 依次获取每个孩子View对象
			View child = getChildAt(i);
			// 获取孩子view的宽高
			int childHeight = child.getMeasuredHeight();
			int childWidth = child.getMeasuredWidth();

			child.layout(0, top, childWidth, top + childHeight);
			// 递增孩子View的top值
			top += childHeight;
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值