android 自定义view
来源:互联网 发布:java 切面 编辑:程序博客网 时间:2024/06/06 02:24
1. 获取屏幕
获取屏幕高,宽,状态栏高度,title高度
DisplayMetrics dm = new DisplayMetrics();
//取得窗口属性
getWindowManager().getDefaultDisplay().getMetrics(dm);
//窗口的宽度
int screenWidth = dm.widthPixels;
//窗口高度
int screenHeight = dm.heightPixels;
// 屏幕密度
float density = dm.density;
int densityDip = dm.densityDpi;
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是上面所求的状态栏的高度
int titleBarHeight = contentTop -statusBarHeight;
textView =(TextView)findViewById(R.id.screen_message);
textView.setText("屏幕宽度: " + screenWidth + "\n屏幕高度: " + screenHeight + "\n 密度:" + density
+ "\n dip密度:" + densityDip + "\n 状态栏:" );2. 获取view 相对于父View 的坐标
mBounds =
new
RectF(getLeft(),getTop(),getRight(),getBottom());
width = mBounds.right - mBounds.left;
height = mBounds.bottom - mBounds.top;
系统不会为wrap_content 测量大小,如果是设置了明确的大小,或是match_parent 那么就不用重写onMesure 了
MeasureSpec的specMode,一共三种类型:
EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
UNSPECIFIED:表示子布局想要多大就多大,很少使用,一般用在scroll view 中。
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);int width;int height ;if (widthMode == MeasureSpec.EXACTLY){width = widthSize;} else{mPaint.setTextSize(mTitleTextSize);mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds);float textWidth = mBounds.width();int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());width = desired;}if (heightMode == MeasureSpec.EXACTLY){height = heightSize;} else{mPaint.setTextSize(mTitleTextSize);mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds);float textHeight = mBounds.height();int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());height = desired;}setMeasuredDimension(width, height);}4. onSizeChange() 重新设置高宽
5. 界面的更新
- Android View---自定义View
- Android View---自定义View
- Android 自定义View 之 自定义View属性
- 【自定义View系列】android自定义View概述
- Android自定义view自定义属性
- Android自定义控件 -- 自定义View
- android自定义view(自定义数字键盘)
- Android自定义View-自定义属性
- Android自定义View-自定义属性
- Android 自定义View
- Android 自定义 View
- android自定义View
- Android 中自定义 view
- android 自定义view组件
- Android 自定义 View
- android 自定义view
- Android:如何自定义View
- android 自定义View
- C语言基础
- Swift-字符串和字符
- 解决魅族layer-list中图片拉伸的bug
- 在Eclipse中在线安装插件SVN和Android studio中配置SVN的一些坑
- 欢迎使用CSDN-markdown编辑器
- android 自定义view
- 解密SparkStreaming运行机制和架构
- JQuery方法查询
- poj 2420 A Star not a Tree?
- 在编辑框(CEdit)控件上追加内容
- css文件内容格式化
- c/c++ 复习随记(4)----fwqlzz love is for ever
- BZOJ 4522: [Cqoi2016]密钥破解
- inline-block和float应区别对待