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

最新文章

  1. [LeetCode] Sequence Reconstruction 序列重建
  2. 51nod1072(wythoff 博弈)
  3. SSH框架
  4. 使用CallerMemberName简化InotifyPropertyChanged的实现
  5. DataSnap如何监控Tcp/IP客户端的连接情况
  6. IText 中文字体解决方案 生成doc文档
  7. [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断
  8. 基于 Android 的 3D 视频样本代码
  9. java 对象比较
  10. UML类图几种关系的总结(网摘)
  11. web框架和django基础(粗糙版)
  12. pybind11 安装
  13. AngularJS 1.x系列:AngularJS服务-Service、Factory、Provider、Value及Constant(5)
  14. iOS开发支付宝支付
  15. 微信小程序中时间转化为时间戳(安卓和苹果兼容性)
  16. java parse 带英文单词的日期字符串 转 date (转化新浪微博api返回的时间)
  17. win7 32位 import cv2 失败 ImportError:DLL load fail:找不到指定模块
  18. windows bat文件运行中文乱码
  19. JavaScript-常用正则函数(适合忘记时看)
  20. ALPS语言学校(西雅图)|ALPS Language School (Seattle)

热门文章

  1. 【15】vuex2.0 之 modules
  2. float 及 overflow 的理解
  3. [CODEVS1051]接龙游戏
  4. WPF 自动选择dll,以SQLite为例
  5. 转 C/C++内存分配方式与存储区
  6. Scrapy学习-23-分布式爬虫
  7. vSphere虚拟化ESXI6.0+vclient安装部署
  8. hdu 4858(简单模拟)
  9. Python Challenge 第十关
  10. windows下apache+php配置 问题总结