自定义View深入了解(一)
来源:互联网 发布:网络诈骗真实案例 编辑:程序博客网 时间:2024/06/07 05:35
在 自定义View(基础)以及自定义(加载动画)中,有人肯定会遇到这样的问题:
1.在布局xml中,设置控件layout_width、layout_height 的值为warp_content时,如:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:custom="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <pw.onlyou.testapplication.ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" custom:firstColor="#F08080" custom:secondColor="#7B68EE" android:background="#aaa" android:layout_centerInParent="true" android:padding="20dp" custom:circleWidth="10dp" custom:speed="10"/></RelativeLayout>
但是呢,人生道路并不是一帆风顺的,看看运行效果图are you kidding me??
砸门把布局文件修改一下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:custom="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <pw.onlyou.testapplication.ProgressBar android:layout_width="200dp" android:layout_height="200dp" custom:firstColor="#F08080" custom:secondColor="#7B68EE" android:layout_centerInParent="true" android:padding="20dp" custom:circleWidth="10dp" custom:speed="10"/></RelativeLayout>
定死宽高,看运行效果:一切正常,但是我是个有追求的人,绝不放过一丁点问题
通过查阅资料(android 开发艺术探索 是一本好书,建议大家看看),原来如此。先看一张图:
这图啥意思??
1.当 View 采用固定宽高时,不管父容器的 MeasureSpec 是什么,View 的 MeasureSpec 都是精确模式,并且大小是LayoutParams 中的大小。
2.当 View 的宽高是 match_parent 时,如果父容器的模式是精确模式,那么 View 也是精确模式,并且大小是父容器的剩余空间;如果父容器是最大模式,那么 View 也是最大模式,并且大小是不会超过父容器的剩余空间。
3.当 View 的宽高是 wrap_content 时,不管父容器的模式是精确模式还是最大模式,View 的模式总是最大模式,并且大小不超过父容器的剩余空间
缩的思内~我们来修改代码
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int WspecMode = MeasureSpec.getMode(widthMeasureSpec); int HspecMode = MeasureSpec.getMode(heightMeasureSpec); if(HspecMode==MeasureSpec.EXACTLY&&WspecMode==MeasureSpec.EXACTLY){ setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)); }else if(WspecMode==MeasureSpec.AT_MOST){ setMeasuredDimension(Width, MeasureSpec.getSize(heightMeasureSpec)); }else if(HspecMode==MeasureSpec.AT_MOST){ setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), Height); } }
Width和Height 分别是我定义的默认大小,当布局控件 大小为warp_content的时候,控件大小分别为Width、Height。
现在我们布局文件大小都是warp_content,看看运行效果:
good,成功了!
还有padding也是要注意的,有时间补上。
文章目前仍在更新中,如果你觉得我的文章有错误或者纰漏,欢迎指正,另外,如果你觉得有用的话,点个赞呗。
1 0
- 自定义View深入了解(一)
- 深入了解View(一、二、三、四)及自定义View
- 深入了解View(一)
- 自定义View控件详解(一) :Android LayoutInflater原理分析,带你一步步深入了解View
- Android layoutInflater连载带入一步一步深入了解View(一)
- 深入了解View(一)——LayoutInflater原理分析
- 深入了解View(一) (二) (三)
- LayoutInflater原理分析,深入了解View(一)
- 深入了解View实现原理以及自定义View详解
- 深入了解View实现原理以及自定义View详解
- Android自定义View的实现方法,深入了解View(四)
- Android 深入(一)- 自定义View之BottomTitleImageView
- android自定义View探索3(onMeasure深入分析一)
- 深入了解View(二)
- 深入了解View(三)
- 深入了解View(四)
- 一步步深入了解View
- 深入了解JSF(一)
- Android Studio脱机工作,变快一些
- 2012年左右做的大白菜winpe启动盘,备份分区了,恢复镜像报错
- App签名和打包相关
- java项目源码 sqlserver数据库springmvc mybatis bootstrap html5
- 按照顺时针的顺序从外向里打印整形数组
- 自定义View深入了解(一)
- SQL Server-字字珠玑,一纸详文,完全理解SERIALIZABLE最高隔离级别(基础系列收尾篇)
- 45. Jump Game II
- eclipse中各图标含义
- 计算机程序设计艺术一线性表“逻辑结构”
- activiti工作流的web流程设计器整合视频教程 SSM和独立部署
- arm64体系架构
- 01python画的玫瑰
- XiaomiRouter自学之路(09-更改串口波特率及Enter键触发)