位置: 编程技术 - 正文

Android自定义之流式布局(android自定义控件入门与实战)

编辑:rootadmin

推荐整理分享Android自定义之流式布局(android自定义控件入门与实战),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:android 自定义spinner,安卓自定义app,android 自定义view三个方法,安卓自定义app,android自定义属性详解,android自定义属性详解,android 自定义view三个方法,android自定义属性详解,内容如对您有帮助,希望把文章链接给更多的朋友!

Android自定义之流式布局(android自定义控件入门与实战)

流式布局,好处就是父类布局可以自动的判断子孩子是不是需要换行,什么时候需要换行,可以做到网页版的标签的效果。今天就是简单的做了自定义的流式布局。

具体效果:原理:其实很简单,Measure Layout。只需要这两个步骤就可以搞定了。完全的手动去Measure Layout。我们看一下代码。解释就在代码里面做注释了,因为使用为知笔记写的博客,&#;式不符合代码&#;式。大家可以看具体的源码。最后又源码下载地址。1.Measure 测量 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int lineHeight = 0 ; int lineWidth = 0 ; int width = 0 ; int height = 0 ; int childCount = getChildCount(); Log.i("Test", getPaddingLeft() &#; "==right=" &#;getPaddingRight()); for (int i = 0; i < childCount; i&#;&#;) { View childView = getChildAt(i); measureChild(childView, widthMeasureSpec, heightMeasureSpec); MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams(); int childWidth = childView.getMeasuredWidth() &#; params.leftMargin &#; params.rightMargin ; int childHeight = childView.getMeasuredHeight() &#; params.topMargin &#; params.bottomMargin ; if ((lineWidth &#; childWidth ) > widthSize - getPaddingLeft() - getPaddingRight() ) { width = Math.max(width, lineWidth); lineWidth = childWidth ; height &#;= lineHeight ; lineHeight = childHeight; }else { lineWidth &#;= childWidth ; lineHeight = Math.max(lineHeight, childHeight); } if (i == childCount-1) { width = Math.max(width, lineWidth); height &#;= lineHeight ; } } height &#;= getPaddingTop() &#; getPaddingBottom() ; setMeasuredDimension(widthMode == MeasureSpec.EXACTLY?widthSize:width, heightMode == MeasureSpec.EXACTLY?heightSize:height); }2.onLayout 布局 @Override protected void onLayout(boolean a, int l, int t, int r, int b) { childViewList.clear(); int childCount = getChildCount() ; int width = getWidth(); int lineWidth = 0 ; int lineHeight = 0 ; List<View> lineViews = new ArrayList<View>(); for (int i = 0; i < childCount; i&#;&#;) { View childView = getChildAt(i); MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams(); int childWidth = childView.getMeasuredWidth() &#; params.leftMargin &#; params.rightMargin ; int childHeight = childView.getMeasuredHeight() &#; params.topMargin &#; params.bottomMargin ; if (lineWidth &#; childWidth > width - getPaddingLeft() - getPaddingRight()) { childViewList.add(lineViews); lineViews = new ArrayList<View>(); if (i == 0 ) { lineHeight &#;= getPaddingTop() ; }else if (i== childCount - 1) { lineHeight &#;= getPaddingBottom() ; } this.lineHeight.add(lineHeight); lineHeight = 0 ; lineWidth = 0 ; } lineWidth &#;= childWidth; lineHeight = Math.max(lineHeight, childHeight) ; lineViews.add(childView); } childViewList.add(lineViews); this.lineHeight.add(lineHeight); int left = getPaddingLeft() ; int top = getPaddingTop(); for (int i = 0; i < childViewList.size(); i&#;&#;) { lineViews = childViewList.get(i); for (int j = 0; j < lineViews.size(); j&#;&#;) { View childView = lineViews.get(j); MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams(); int lc = left &#; params.leftMargin ; int tc = top &#; params.topMargin ; int rc = lc &#; childView.getMeasuredWidth() ; int bc = tc &#; childView.getMeasuredHeight() ; childView.layout(lc,tc,rc,bc); left &#;= params.leftMargin &#; childView.getMeasuredWidth() &#; params.rightMargin ; } left = getPaddingLeft() ; top &#;= this.lineHeight.get(i) ; } }代码下载地址: 百度网盘:

版权声明:本文为博主原创文章,未经博主允许不得转载。

WebView控件应用——模仿URL有道词典 一、项目开发1、开发介绍本项目是用WebView控件模仿网上有道词典,WebSettings中有很多关于WebView的设置,这里列出几个常用的方法:1)、//得到WebSettings

android开发步步为营之:解决ScrollView和ListView触摸事件onInterceptTouchEvent相互冲突问题 最近项目里面有个需求,一个页面放了一个ScrollView,整个页面可以向上滚动,然后ScrollView里面又嵌套了一个ListView,ListView里面的数据也是可以上下滑

Android开源SlidingMenu的使用 SlidingMenu是Android上的一个侧滑菜单,如下图所示:今天主要记录的是SlidingMenu的使用,我们可以从Github上下载该开源项目,下载地址是:

标签: android自定义控件入门与实战

本文链接地址:https://www.jiuchutong.com/biancheng/371007.html 转载请保留说明!

上一篇:Android学习【Android内核编译流程和错误笔记】(android入门视频教程)

下一篇:WebView控件应用——模仿URL有道词典(web ui控件)

  • 商品流通企业税种有哪些
  • 建筑业如何分项目核算
  • 个体户定额多少不用交税
  • 个人所得税申报操作流程
  • 企业卫生费是指什么
  • 特殊销售业务包括什么
  • 上年度亏损用什么弥补
  • 银行 收美金
  • 公司购买服装
  • 分摊材料成本差异的会计处理
  • 库存商品一定是存货吗
  • 土地使用权摊销的会计分录
  • 发票是不是都一样
  • 外购低值易耗品可以抵扣进项税额吗
  • 出差伙食补贴没发
  • 酒店自开农副产品收购发票需要缴税吗
  • 多提税金的具体分录
  • 床垫增值税税率是多少
  • 成本是否有含税金额
  • 一般纳税人附加税费减免政策
  • 税务局增项
  • 筹建期的餐饮费会计分录
  • 无需付款的其他应付款怎么做凭证?
  • 现金流量表的计算公式原理
  • 结转净利润到利润分配
  • 已认证的发票退税怎么退
  • 如何申请税务发票
  • 代理买卖证券款属于什么科目
  • 银行汇票和银行本票的区别
  • 国税打印发票
  • php的二维数组
  • nerf 入门
  • 应税货物及劳务无法录入
  • vue项目内html
  • typescript和javascript那个更高级
  • vmware vmtools
  • yii框架教程
  • 员工离职补偿怎么入账
  • 公司注销退还股票流程
  • mongodb起源
  • 织梦标签理解
  • 支付工程款的会计科目怎么写
  • 货币资金的计税基础
  • c#窗体类属性和方法
  • 消费税是指怎样的收入
  • 会计核算的内容包括
  • 工程预付税金如何计算
  • 创业带动就业补贴是给企业的还是员工的
  • 退休人员基本养老金包括哪些
  • 两个公司的出纳怎么分账
  • 报名费无发票要补交吗
  • 饭店的会计做账怎么做
  • 平均增长率计算公式怎么算
  • 会计账簿按账页不同可以分为
  • 固定资产折旧怎么算
  • sql触发器菜鸟教程
  • mac80211源码分析
  • windows 9
  • window10预览在哪里找
  • win7怎么添加此电脑
  • 重装xp系统后键盘没反应
  • xp系统分区工具
  • linux操作系统百度百科
  • ubuntu唤醒快捷键
  • WIN10系统中没有接入音频设备 要启动gui
  • 电脑系统垃圾
  • win7 ctrl+alt+del
  • 检查linux是否安装了,可用哪些命令
  • webgl基础教程
  • bootstrap基础
  • 完美解决gvim的菜单乱码问题
  • 文件名可以包含*吗
  • unity反射效果
  • js函数function用法
  • python list set dict tuple
  • javascript新手教程
  • jquery的gt
  • js如何使用
  • jqueryon事件
  • 银元面值有哪些种类
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设