自己定义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;
		}
	}
}
时间: 2024-08-28 19:30:25

自己定义ViewGroup控件(一)-----&gt;流式布局进阶(一)的相关文章

自己定义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控件(二)-----&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

Android UI-自定义日历控件

Android UI-自定义日历控件 本篇博客笔者给大家分享一个日历控件,这里有个需求:要求显示当前月的日期,左右可以切换月份来查看日期. 我们想一想会如何去实现这样的一个控件,有开源的,但可能不太满足我们的特定的需求,这里笔者自定义了一个,读者可以根据自己的需求来修改代码.下面来说一下实现的思路: 首先我们要显示当前月份,自然我们要计算出当前的日期,并且把每一天对应到具体的星期,我们会有以下效果: 我们先想一下这样的效果用什么控件可以实现?很自然可以想到用网格视图GridView,但这里笔者使

android 自己定义组合控件

自己定义控件是一些android程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲下自己定义组合控件,这个特别适合在标题栏或者设置界面,看以下图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29kZXJpbmNoaW5h/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/7

自己定义滑动开关控件的实现与使用

在IPhone中,滑动开关控件很常见,并且效果也很好,可是在Android平台下,却没有自带的这样的控件,仅仅有功能类似的ToggleButton控件.本篇文章主要介绍自己定义的滑动开关控件的实现与使用.在实现的过程中,也參考了其它类似自己定义控件的实现,同一时候对代码进行了优化. 首先看实现的效果图 以下解说这个自己定义控件怎样实现 /** * 滑动控件 * * @Time 2014-6-17 下午2:35:17 */ public class SlipSwitch extends View

安卓自己定义日历控件

尊重作者劳动成果.转载请注明出处:http://blog.csdn.net/baiyuliang2013/article/details/37732149 近期,因工作须要,须要实现自己定义日历控件功能,主要应用于软件中的酒店入住时间选择功能.进入日历后,可选择入住时间,及离开时间,选择完毕后,再次进入日历时.会显示上次选中的结果,默认选择日期是在距当前日期三个月内.三个月以外的均以灰色显示.且不可点击.本篇实现的效果是高仿某软件的界面效果: 某软件界面效果: watermark/2/text/

自定义ViewGroup 流式布局

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

在Winform界面使用自定义用户控件及TabelPanel和StackPanel布局控件

在很多时候,我们做一些非常规化的界面的时候,往往需要创建一些用户控件,在其中绘制好一些基础的界面块,作为后续重复使用的一个单元,用户控件同时也可以封装处理一些简单的逻辑.在开发Winform各种类型项目,我都时不时需要定制一些特殊的用户控件,以方便在界面模块中反复使用.我们一般是在自定义的用户控件里面,添加各种各样的界面控件元素,或者封装一些特殊的函数处理共外部调用等.本篇随笔主要介绍基于DevExpress的Winform开发经验,介绍一个类似看板信息的用户控件,并在TabelLayout和S