自己定义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;
}
}
}
自己定义ViewGroup控件(二)----->流式布局进阶(二)的更多相关文章
- 自己定义ViewGroup控件(一)----->流式布局进阶(一)
main.xml <? xml version="1.0" encoding="utf-8"?> <com.example.SimpleLay ...
- 05-移动web之流式布局
一.视口 1.常见屏幕知识 设备 解释 描述 宽 屏幕的宽度 - (单位:英寸) 屏幕的宽度 高 屏幕的高度 -(单位:英寸) 屏幕的高度 对角线 屏幕的对角线的长度 英寸 一般说手机尺寸 是指以屏幕 ...
- Android控件进阶-自定义流式布局和热门标签控件
技术:Android+java 概述 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧,类 ...
- android 自己定义组合控件
自己定义控件是一些android程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲 ...
- Quartz2D-二维画图引擎 、自己定义UI控件
// // MyDraw.m // 绘图 #import "MyDraw.h" @implementation MyDraw //Quartz2D 是一个二维绘图引擎 //自己定义 ...
- android:自己定义组合控件Weight(高仿猫眼底部菜单条)
在我们实际开发其中.会碰见一些布局结构类似或者同样的界面.比如应用的设置界面.tabbutton界面等. 这时候.对于刚開始学习的人来说,xml里面一个个绘制出来也许是最初的想法.可能随着经验的积累, ...
- Android 使用shape定义不同控件的的颜色、背景色、边框色
Android 使用shape定义不同控件的的颜色.背景色.边框色 设置按钮的右边框和底边框颜色为红色,边框大小为3dp: 在drawable新建一个 buttonstyle.xml的文件,内容如下: ...
- MUI框架-03-自定义MUI控件样式
MUI框架-03-自定义MUI控件样式 开发请查阅:官方文档:http://dev.dcloud.net.cn/mui/ui/ 如何自定义MUI控件样式 mui 以 iOS 7的 UI 为基础,补充了 ...
- android自己定义开关控件
近日在android项目要使用开关控件.可是android中自带的开关控件不太惬意,所以就打算通过自己定义View写一个开关控件 ios的开关控件当然就是我要仿照的目标. 先上图: waterma ...
随机推荐
- [LeetCode] Compare Version Numbers 字符串操作
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- django + gunicorn + supervisor
在服务器上跑着一个Django项目,想用supervisor管理起来,遇到一个小问题,记录一下本来启动Django项目的命令是用的manage.py , 但是这中方法有个很神奇的坑,就是ctrl + ...
- 安装聊天软件telegram-cli
Telegram是一款加密通信的聊天软件,可以在linux,windows,android,chrome等运行.官方网址:https://telegram.org/ 它是有桌面版的,但作为一个linu ...
- Python 函数的一般形式及参数
#!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/01 21:46 # @Author : lijunjiang # @Fi ...
- 倒计时器CountDownLatch与同步屏障CyclicBarrier
CountDownLatch CountDownLatch是一个非常实用的多线程控制工具类,这个工具通常用来控制线程等待,它可以让某一个线程等待直到倒计时结束,再开始执行.在这里指CountDownL ...
- hdu 2363(枚举+最短路好题)
Cycling Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- LeetCode OJ-- Combination Sum II **
https://oj.leetcode.com/problems/combination-sum-ii/ 一列数,每个数只能用一次或者不用,给出和为target的组合. 递归写的深搜,使用了编程技巧, ...
- CCCC L1-002. 打印沙漏【图形打印】
L1-002. 打印沙漏 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给 ...
- 洛谷 P1426 小鱼会有危险吗【模拟/题意理解】
题目描述 有一次,小鱼要从A处沿直线往右边游,小鱼第一秒可以游7米,从第二秒开始每秒游的距离只有前一秒的98%.有个极其邪恶的猎人在距离A处右边s米的地方,安装了一个隐蔽的探测器,探测器左右x米之内是 ...
- jzyzoj 栈——P1148:括号匹配加强版
括号匹配加强版 描述 Description 对于一个由(,),[,]括号组成的字符串,求出其中最长的括号匹配字串. 具体来说,满足如下条件的字符串成为括号匹配的字符串: (1) (),[] 是括号匹 ...