自己定义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;
}
}
}
自己定义ViewGroup控件(一)----->流式布局进阶(一)的更多相关文章
- 自己定义ViewGroup控件(二)----->流式布局进阶(二)
main.xml <?xml version="1.0" encoding="utf-8"? > <com.example.SimpleLay ...
- Android控件进阶-自定义流式布局和热门标签控件
技术:Android+java 概述 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧,类 ...
- android 自己定义组合控件
自己定义控件是一些android程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲 ...
- android:自己定义组合控件Weight(高仿猫眼底部菜单条)
在我们实际开发其中.会碰见一些布局结构类似或者同样的界面.比如应用的设置界面.tabbutton界面等. 这时候.对于刚開始学习的人来说,xml里面一个个绘制出来也许是最初的想法.可能随着经验的积累, ...
- Android 使用shape定义不同控件的的颜色、背景色、边框色
Android 使用shape定义不同控件的的颜色.背景色.边框色 设置按钮的右边框和底边框颜色为红色,边框大小为3dp: 在drawable新建一个 buttonstyle.xml的文件,内容如下: ...
- Quartz2D-二维画图引擎 、自己定义UI控件
// // MyDraw.m // 绘图 #import "MyDraw.h" @implementation MyDraw //Quartz2D 是一个二维绘图引擎 //自己定义 ...
- MUI框架-03-自定义MUI控件样式
MUI框架-03-自定义MUI控件样式 开发请查阅:官方文档:http://dev.dcloud.net.cn/mui/ui/ 如何自定义MUI控件样式 mui 以 iOS 7的 UI 为基础,补充了 ...
- android自己定义开关控件
近日在android项目要使用开关控件.可是android中自带的开关控件不太惬意,所以就打算通过自己定义View写一个开关控件 ios的开关控件当然就是我要仿照的目标. 先上图: waterma ...
- WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性
原文:WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性 如果你要自定义一个图片按钮控件,那么如何在主窗体绑定这个控件上图片的Source呢? ...
随机推荐
- [0] Hello World
受不了CSDN了,广告多,慢,编辑器难用,还限制博客数量.
- KBE_创建项目和基本常识
此笔记参考官方文档 第一个项目 资产库:是每一个项目文件夹的名称,使用KBE提供的生成工具生成一个最小资产库,其中包含了很多常用的工具,默认名server_assets: res:放置一些资源(入地图 ...
- 又是latch: cache buffers chains惹得祸
前言 一大早,客户给我打电话说: xx,应用很慢,查询数据总是超时,让我看看... 根据多年DBA经验,首当其冲的肯定是去查询数据库在这段时间都在干嘛. 分析 导出awr报告分析 1). 数据库在此时 ...
- 每日命令:(12)sar
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告, 包括:文件的读写情况.系统调用的使用 ...
- linux命令 dig-域名查询工具
博主推荐:更多网络测试相关命令关注 网络测试 收藏linux命令大全 dig命令是常用的域名查询工具,可以用来测试域名系统工作是否正常. 语法 dig(选项)(参数) 选项 @<服务器地址&g ...
- uva 10596 - Morning Walk
Problem H Morning Walk Time Limit 3 Seconds Kamal is a Motashota guy. He has got a new job in Chitta ...
- AutoEncoders原理
目录 Auto-Encoders How to Train? Auto-Encoders How to Train?
- The Text Splitting (将字符串分成若干份,每份长度为p或q)
Description You are given the string s of length n and the numbers p, q. Split the string s to piece ...
- PHP学习笔记<参数的传递>
简单的例子说明参数在PHP文件之间的传递(有两个PHP文件在index.php文件上点击链接,在跳转的时候,依据参数的不同在neirong.php文件中显示不同的内容) inde.php的内容如下: ...
- Spark 动态(统一)内存管理模型
作者编辑:王玮,胡玉林 一.回顾 在前面的一篇文章中我们介绍了spark静态内存管理模式以及相关知识https://blog.csdn.net/anitinaj/article/details/809 ...