自定义View-2-重写onMeasure
2016-04-23 11:19
302 查看
效果图
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:background="#003839" android:gravity="center" android:visibility="visible" android:text="SECOND"/> <com.pengkv.apple.weight.FirstView android:layout_width="wrap_content" android:visibility="visible" android:background="#888787" android:layout_height="wrap_content"/> </LinearLayout>
View代码
public class FirstView extends LinearLayout { public FirstView(Context context) {//代码实例化的时候调用 super(context); } public FirstView(Context context, AttributeSet attrs) {//布局文件引用的时候调用 super(context, attrs); } public FirstView(Context context, AttributeSet attrs, int defStyleAttr) {//自定义属性值的时候调用 super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec,heightMeasureSpec); setMeasuredDimension(measureSpec(widthMeasureSpec),measureSpec(heightMeasureSpec)); //重测尺寸 } public int measureSpec(int measureSpec){ int result=0; int specMode=MeasureSpec.getMode(measureSpec);//获取测量模式 int specSize=MeasureSpec.getSize(measureSpec);//获取测量尺寸 if (specMode==MeasureSpec.EXACTLY){//精确模式:包括准确设置dp值和match_parent result=specSize; }else { result=400;//默认设置的尺寸 if (specMode==MeasureSpec.AT_MOST){ result=Math.min(result,specSize); } } return result; } }
相关文章推荐
- 自定义View-1-动态添加view
- 机器学习------平台和语言选择
- Android前端人员与后台开发的撕逼(一)
- rsync常见错误总结
- POJ 1328 Radar Installation 题解
- 欢迎使用CSDN-markdown编辑器
- [从头学数学] 第191节 空间向量与立体几何
- jQuery Easing 使用方法及其图解
- 自定义View-序言-你需要的知识点
- 【.net深呼吸】非 Web 项目使用缓存
- IOS-HTTP协议
- Ubuntu14.04中文版调出五笔输入法关键步骤
- scatter_matrix&df.plot&sns.boxplot
- iOS之UIView的动画
- centos6.5 mysql开机启动
- 进度条07
- android 6.0权限管理
- 要想生活的好就要努力努力在努力!!!
- Android学习笔记(Google官方教程)(六)
- Android学习笔记(Google官方教程)(五)