项目中需要自定义一个密码键盘,不能导入xml文件,只能动态添加控件到ViewGroup中。

指定了ViewGroup的大小,重新计算子控件的宽高。但计算子控件的宽高一直错误,导致子控件的文字一直无法居中,查找了不少资料,终于解决了问题,特此记录。

1、添加控件

void initView() {for (int i = 0; i < 13; i++) {TextView button = new TextView(context);button.setLayoutParams(new LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));button.setBackgroundColor(Color.WHITE);button.setGravity(Gravity.CENTER);button.setTextColor(Color.BLACK); button.setTextSize(22);if (i == 3) {button.setText("Del");button.setBackgroundColor(Color.YELLOW);button.setTextColor(Color.WHITE);} else if (i == 7) {button.setText("Enter");button.setBackgroundColor(Color.GREEN);button.setTextColor(Color.WHITE);} else if (i == 11) {button.setText("Cancel");button.setBackgroundColor(Color.RED);button.setTextColor(Color.WHITE);} else {button.setText("" + i);}button.setTag("" + i);addView(button);}TextView textView = new TextView(context);textView.setLayoutParams(new LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));textView.setBackgroundColor(Color.RED);textView.setGravity(Gravity.CENTER);textView.setTextColor(Color.BLACK);textView.setTextSize(22);addView(textView);}

2、计算宽高

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int parentWidth = MeasureSpec.getSize(widthMeasureSpec);int parentHeight = MeasureSpec.getSize(heightMeasureSpec);int childWidth = (parentWidth - getPaddingLeft() - getPaddingRight() - 3) / columns;int childHeight = (parentHeight - getPaddingTop() - getPaddingBottom() - 4) / (rows + 1);int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);int childWidthMeasureSpec2 = MeasureSpec.makeMeasureSpec(childWidth * 2 + 1, MeasureSpec.EXACTLY);int childHeightMeasureSpec2 = MeasureSpec.makeMeasureSpec(childHeight * 2 + 1, MeasureSpec.EXACTLY);int childWidthMeasureSpec3 = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY);int childCount = getChildCount();for (int i = 0; i < childCount; i++) {View child = getChildAt(i);LayoutParams lp = child.getLayoutParams();if (i == 3 || i == 7) {measureChild(child, childWidthMeasureSpec, childHeightMeasureSpec2);child.measure(childWidthMeasureSpec, childHeightMeasureSpec2);} else if (i == 12) {measureChild(child, childWidthMeasureSpec2, childHeightMeasureSpec);child.measure(childWidthMeasureSpec2, childHeightMeasureSpec);} else if (i == 13) {measureChild(child, childWidthMeasureSpec3, childHeightMeasureSpec);child.measure(childWidthMeasureSpec3, childHeightMeasureSpec);} else {measureChild(child, childWidthMeasureSpec, childHeightMeasureSpec);child.measure(childWidthMeasureSpec, childHeightMeasureSpec);}}setMeasuredDimension(parentWidth, parentHeight);}

给子控件计算出宽高,重写onLayout方法开始布局

protected void onLayout(boolean changed, int l, int t, int r, int b){int height = b - t;int width = r - l;int gridW = width / columns;int gridH = height / (rows + 1);int inputH = gridH;int left = 0;int top = 1;View child;int index;top += inputH;for (int i = 0; i < rows; i++) {for (int j = 0; j < columns; j++) {index = i * columns + j;child = this.getChildAt(index);if (child == null) return;//Log.i(TAG, "index = " + index + " child.getWidth() = " + child.getMeasuredWidth() + " child.getHeight() = " + child.getMeasuredHeight());if (index == 13) {child.layout(3, 0, child.getMeasuredWidth(), child.getMeasuredHeight());break;}int ordel = i * columns + j;switch (ordel) {case 3:// deletechild.layout(3 + 3 * gridW, top, 3 * gridW + gridW, 2 * gridH + 2 + inputH);break;case 7:// enterchild.layout(3 + 3 * gridW, 3 + 2 * gridH + inputH, 3 * gridW + gridW, 4 * gridH + inputH);break;case 11:// cancelchild.layout(0, 3 + 3 * gridH + 1 + inputH, gridW, 4 * gridH + inputH);break;case 12:// 0child.layout(gridW + 1, 3 + 3 * gridH + 1 + inputH, 2 + gridW + 2 * gridW, 4 * gridH + inputH);break;default: left = j * gridW + j;child.layout(left, top, left + gridW, top + gridH);break;}}top += gridH + 1;}}

参考文章:https://www.jianshu.com/p/12d37bf44e60

Android 指定宽高的ViewGroup相关推荐

  1. java生成缩略图,接收图片,按指定宽高或按比例生成缩略图

    全栈工程师开发手册 (作者:栾鹏) java教程全解 java生成缩略图,接收图片,按指定宽高或按比例生成缩略图,能有效降低前端访问图片的流量负载. 支持的图片类型 : [BMP, bmp, jpg, ...

  2. 安卓android按宽/高缩放图片到指定大小并进行裁剪得到中间部分图片

    /*** 按宽/高缩放图片到指定大小并进行裁剪得到中间部分图片 <br>* 方 法 名:zoomBitmap <br>* 创 建 人:楼翔宇 <br>* 创建时间: ...

  3. java jsoup抓取百度图片保存到本地,并压缩到指定大小(可指定宽高或者流大小)

    代码如下: package cn.xyz.commons.utils; import java.awt.image.BufferedImage; import java.io.ByteArrayInp ...

  4. android mp4宽高,Android:MediaPlayer视频宽高比问题

    我遇到了一个我无法解决的问题.我正在使用MediaPlayer类来播放视频;但是,我总是失去原有的宽高比.这里提到了媒体播放器代码和xml代码.我尝试了很多东西,但仍然无法解决问题.我需要保持原始的宽 ...

  5. 原生图片预览实现及由此引出的图片自适应宽高问题探索

    看到手机相册,突发奇想:能不能用实现一个原生的页面"相册"?或者说,传统网页中怎么实现图片预览功能? 如果在原生网页中使用插件,那必不可少的要引入一堆东西(并不是鄙视插件,只是为了 ...

  6. 《CSS 世界》读书笔记-流与宽高

    大厂技术  坚持周更  精选好文 1. 前言 在学习 CSS 的过程中,我经常会被数不清的属性和特性弄得晕头转向.作为前端新手,经常会坐在显示器前花很多很多时间去 "追" 视觉稿, ...

  7. 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件

    博客地址 : http://blog.csdn.net/shulianghan/article/details/41520569 代码下载 : -- GitHub : https://github.c ...

  8. Android获取UI控件的宽高

    一.概述 我们如果开发一些依赖于UI控件宽高的功能,那么自然会想到调用View的getHeigth()和getWidth()方法,对于新手来说,这里有一个坑,你在Activity中的onCreate( ...

  9. android layoutinflater 高度,探究LayoutInflater和RecyclerView中item设置宽高无效

    1. LayoutInflater是做什么的 Instantiates a layout XML file into its corresponding {@link android.view.Vie ...

  10. android 自定义控件的宽高_巧用Handler获取View控件信息

    众所周知,在Android实际开发中,对于某些复杂多变的情况,控件的位置摆放.大小控制并非是xml类型的layout文件完全可以搞定的.此时,我们通常会使用Java代码来通过动态计算,将指定的控件摆放 ...

最新文章

  1. 【Groovy】字符串 ( 字符串注入函数 | asBoolean | execute | minus )
  2. Servlet的部分request请求处理
  3. linux飞信机器人的安装fetion
  4. (批量)备份github仓库到本地
  5. 随笔2 PAT1001.A+B Format (20)
  6. python控制语句实验报告,python流程控制语句案例练习
  7. 一些SharePoint 2007开发的在线课程
  8. 计算机一直在启动修复怎么关机,电脑开机总是要启动修复修复后重启还要修复怎么处理方法...
  9. 合并排序时间复杂度推导
  10. Java字符串的字符进行排序
  11. 皮克定理(计算多边形面积)
  12. 【论文】写毕业论文过程中遇到的问题及解决方式记录
  13. 波兰计算机留学,波兰(Rzeczpospolita Polska)留学申请全面解析
  14. android手机电视下载软件安装失败,新买的电视无法安装第三方软件?方法汇总来了,解决99%的问题...
  15. 【暑期每日一题】洛谷 P1192 台阶问题
  16. HarmonyOS第三方组件——鸿蒙图片裁剪组件ohos-Image-Cropper
  17. Mtk Camera中Hal1/Hal3的Picture size和Preview size配置
  18. java设计九宫格拼图软件哪个好用_抖音超火的朋友圈九宫格用什么软件做的? 抖音九宫格图片制作教程...
  19. Pandas学习笔记(二)—— Pandas索引
  20. ListView动态刷新某项Item

热门文章

  1. codeforces——962
  2. linux如何破解密码
  3. 01-Sass 环境搭建与基本语法
  4. python爬虫代理ip
  5. word修改或删除脚注或尾注分隔符
  6. DDS每个数据包和域ID大小的数据开销
  7. C++ 在栈上分配内存
  8. A pretty carpet
  9. 微信对账单 java_微信下载对账单
  10. Trafodion之DTM介绍