自己定义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_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 {@Overridepublic 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(至多)。子元素至多达到指定大小的值。*/@Overrideprotected 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()* 过程结束后才干获取到。再重申一遍*/@Overrideprotected 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;}}
}


转载于:https://www.cnblogs.com/liguangsunls/p/7233445.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/416325.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

oracle计算表的大小,简要分析估算oracle表的大小

查询oracle表的大小有几种方法&#xff0c;笔者简要分析下他们的异同环境&#xff0c;newsadmin.newlog&#xff0c;原本有244,459,078条记录&#xff0c;delete后&#xff0c;现在只有51,109,919记录。一、segmentsselect bytes/1024/1024/1024 from dba_segments where segme…

NOIP2007 count 统计数字

问题描述 某次科研调查时得到了n个自然数&#xff0c;每个数均不超过1.5109。已知不相同的数不会超过10000个&#xff0c;现在需要统计这些自然数各自出现的个数&#xff0c;并按照自然数从小到大的顺序输出统计结果。 输入 输入文件count.in包含n1行&#xff1b; 第一行是…

【C++11/17】std::map高效插入

我们在使用stl的映射容器std::map时&#xff0c;经常需要向容器中插入数据。由于map的元素key值是唯一的&#xff0c;我们经常遇到这样的场景&#xff1a; 向map中插入元素时&#xff0c;指定的key已经存在则直接更新&#xff1b;指定的key不存在&#xff0c;然后才做插入操作…

oracle 监听 无法连接,oracle监听hang,无法建立新连接TNS-12540

TNS-12518: TNS:listener could not hand off client connectionTNS-12540: TNS:internal limit restriction exceeded监听无法建立新连接&#xff0c;报如上错误&#xff0c;请教各位大神是什么原因&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;…

pgd 游戏教程 基地

http://www.pascalgamedevelopment.com/content.php?417-Castle-Game-Engine-6-2-released转载于:https://www.cnblogs.com/delphi-xe5/p/7237464.html

oracle选择外键列,Oracle外键列上是否需要索引?

外键列上缺少索引会带来两个问题&#xff0c;限制并发性、影响性能。而这两个问题中的任意一个都可能会造成严重性能问题。 无论是Or外键列上缺少索引会带来两个问题&#xff0c;限制并发性、影响性能。而这两个问题中的任意一个都可能会造成严重性能问题。无论是Oracle的官方文…

select * 和select 所有字段的区别

文章取自http://blog.csdn.net/u014305991/article/details/44964171 MySQL 5.1.37 表记录数41,547,002&#xff0c;即4000w行 使用远程客户端取1000条数据&#xff0c;统计时间&#xff1a; SELECT * FROM dmsp.dmsp_dimension_content LIMIT 0, 1000; 时间2.218s&#xff0c;…

php的strpos不支持数字,php使用strpos判断字符串中数字类型子字符串出错的解决方法 原创...

php使用strpos判断字符串中数字类型子字符串出错的解决方法 原创这里有新鲜出炉的 PHP 面向对象编程&#xff0c;程序狗速度看过来&#xff01;PHP 开源脚本语言PHP(外文名: Hypertext Preprocessor&#xff0c;中文名&#xff1a;"超文本预处理器")是一种通用开源脚…

css动画详解 (transition animation)

属性 transition&#xff08;4个属性&#xff09;: transition: width 5s ease 3s; /*简写*/transition-property: width; /*过渡属性名*/transition-duration: 5s; /*持续时间&#xff0c;默认为0*/transition-timing-function: ease; /*速度*/transition-delay: 3s; /*延迟*…

php ajax session死锁,session过期,ajax请求处理

session会话过期&#xff0c;如果是请求。可以直接定位到页面。如果是ajax请求。无法跳转到页面。可以通过如下方式实现&#xff0c;在Response的输出流里面&#xff0c;向前端写一段html代码来实现。public boolean preHandle(HttpServletRequest request, HttpServletRespons…

NYOJ题目 263 精挑细选

题目描述&#xff1a; 小王是公司的仓库管理员&#xff0c;一天&#xff0c;他接到了这样一个任务&#xff1a;从仓库中找出一根钢管。这听起来不算什么&#xff0c;但是这根钢管的要求可真是让他犯难了&#xff0c;要求如下&#xff1a;1、 这根钢管一定要是仓库中最长的&…

充电桩系统php源码,源码 充电桩程序设计 - 下载 - 搜珍网

压缩包 : e0f671190e843b596bf8d7dfd98914.zip 列表源码/00/源码/00/delay.crf源码/00/delay.d源码/00/delay.o源码/00/gui.crf源码/00/gui.d源码/00/gui.o源码/00/inc/源码/00/inc/delay.h源码/00/inc/io_bit.h源码/00/inc/main.h源码/00/inc/stm32f10x_conf.h源码/00/inc/stm…

mysql中max_allowed_packet参数的配置方法(避免大数据写入或者更新失败)

MySQL根据配置文件会限制Server接受的数据包大小。有时候大的插入和更新会受 max_allowed_packet 参数限制&#xff0c;导致写入或者更新失败。查看目前配置&#xff1a; show VARIABLES like %max_allowed_packet%; 显示的结果为&#xff1a; ----------------------------- |…

linux命令行sip电话,基于Linux和MiniGUI的SIP电话终端设计

0 引言随着VoIP的迅猛发展&#xff0c;越来越多的个人用户正在使用软件电话、IP电话通过VoIP系统拨打国内和国际长途&#xff0c;IP电话的需求量越来越大&#xff0c;同时&#xff0c;人们对IP电话的要求也越来越高&#xff0c;例如要求IP电话体积小、方便携带、功耗低、待机时…