自定义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" >
<!-- 在XML中添加上layout_margin参数 -->
    <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 {
	/**
	 * 构造函数--二话不说,直接写出三个来
	 *
	 * @param context
	 */
	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支持子控件的layout_margin参数,
	 * 则自定义的ViewGroup类必须重载generateLayoutParams
	 * ()函数,并且在该函数中返回一个ViewGroup.MarginLayoutParams派生类对象,这样才能使用margin参数。
	 */
	@Override
	protected LayoutParams generateLayoutParams(LayoutParams p) {
		return new MarginLayoutParams(p);
	}

	/**
	 * 从指定的XML中获取对应的layout_width和layout_height值
	 */
	// 如果我们还需要margin相关的参数就只能重写generateLayoutParams()函数了:
	@Override
	public LayoutParams generateLayoutParams(AttributeSet attrs) {
		return new MarginLayoutParams(getContext(), attrs);
	}

	/**
	 * generateDefaultLayoutParams()函数。 直接返回对应的MarginLayoutParams()的实例
	 */
	/**
	 * 如果要使用默认的构造方法,就生成layout_width="wrap_content"、layout_height="wrap_content"
	 * 对应的参数
	 */
	/**
	 * 为什么非要重写generateLayoutParams()函数了,就是因为默认的generateLayoutParams()
	 * 函数只会提取layout_width
	 * 、layout_height的值,只有MarginLayoutParams()才具有提取margin间距的功能!!!!
	 */
	@Override
	protected LayoutParams generateDefaultLayoutParams() {
		return new MarginLayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}

	/**
	 * 此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);

			// 获取MarginLayoutParams布局参数!!!!!!!!!!!!!!!!!!!!!!!
			/**
			 * 由于generateLayoutParams()的返回值是LayoutParams实例,
			 * 而MarginLayoutParams是派生自LayoutParam的
			 * ;所以根据类的多态的特性,可以直接将此时的LayoutParams实例直接强转成MarginLayoutParams实例;
			 * 所以下面这句在这里是不会报错的:
			 */
			MarginLayoutParams lp = (MarginLayoutParams) child
					.getLayoutParams();
			int childHeight = child.getMeasuredHeight() + lp.topMargin
					+ lp.bottomMargin;
			int childWidth = child.getMeasuredWidth() + lp.leftMargin
					+ lp.rightMargin;

			// 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);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int top = 0;
		int count = getChildCount();
		for (int i = 0; i < count; i++) {

			View child = getChildAt(i);
			// 获取MarginLayoutParams布局参数!!!!!!!!!!!!!!!!!!!!
			MarginLayoutParams lp = (MarginLayoutParams) child
					.getLayoutParams();
			int childHeight = child.getMeasuredHeight() + lp.topMargin
					+ lp.bottomMargin;
			int childWidth = child.getMeasuredWidth() + lp.leftMargin
					+ lp.rightMargin;

			child.layout(0, top, childWidth, top + childHeight);
			top += childHeight;
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 03:21:35

自定义ViewGroup控件(二)----->流式布局进阶(二)的相关文章

自己定义ViewGroup控件(二)-----&amp;gt;流式布局进阶(二)

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:layou

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

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

自己定义ViewGroup控件(一)-----&amp;gt;流式布局进阶(一)

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:layou

自定义ViewGroup控件--自定义属性(面板思想)

attrs.xml(第一步,在这里) <?xml version="1.0" encoding="utf-8"?> <resources> <!-- R.styleable.TopBar_titleTextColor在R文件中是这样的形式 --> <declare-styleable name="TopBar"> <!-- 文字 --> <attr name="leftT

Android自定义用户控件简单范例(二)

对于完全由后台定制的控件,并不是很方便其他人的使用,因为我们常常需要看到控件放到xml界面上的效果,并根据效果进行布局的调整,这就需要一个更加标准的控件制作流程: 我们的自定义控件和其他的控件一样,应该写成一个类,而这个类的属性是是有自己来决定的. 我们要在res/values目录下建立一个attrs.xml的文件,并在此文件中增加对控件的属性的定义. 使用AttributeSet来完成控件类的构造函数,并在构造函数中将自定义控件类中变量与attrs.xml中的属性连接起来. 在自定义控件类中使

安卓流式布局

一.流式布局效果 二.工程结构 三.新建工程,自定义GroupView(流式布局) package com.yuanlei.flowlayoutdemo; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import java.util.ArrayLi

流式布局的实现-1

流式布局可以实现逐行填满的布局效果:适用于关键词搜索和热门展示,可以动态的添加标签,用起来十分方便与快捷 源码下载(由慕课网的老师提供,谢谢) 之后说说主要的安排: 第一篇:创建类,确定继承关系,实现构造函数,确定成员函数: 第二篇:实现FlowLayout(流式布局)主要函数的方法: 第一篇:创建类,确定继承关系,实现构造函数,确定成员函数: 第二篇与之后几篇:实现各函数,并说明成员变量的作用; 和用listView实现下拉刷新一样,还是先分析文件结构: 包括了两个类: public clas

自定义ViewGroup 流式布局

使用 public class MainActivity extends Activity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_flowlayout);         FlowLayout flow_layout = 

Android自定义ViewGroup实现流式布局

实现宽度不足自动换行的流式布局: FlowLayout.java package com.jackie.flowlayout; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; /** * Created by Jackie on 8/28/15. */ public class FlowLayout