自定义view中用到了两个类--获取屏幕信息、设置布局
2016-02-28 10:19
501 查看
自定义view有三种情况,1.绘制view,2.组合控件,3.继承父类。下面介绍一下我在组合控件时用过的两个类。
1.DisplayMetrics
2.LayoutParams
To access the DisplayMetrics members, initialize an object like this
这个类是获取手机屏幕信息的一个类,例如尺寸、分辨率和字体尺寸(自己翻译的文档,不对请指正),初始化DisplayMetrics方法如下:
getWindowManager().getDefaultDisplay().getMetrics(metrics);此时metrics是有当前屏幕信息的。包括分辨率、长、宽、字体分辨率等信息。
LayoutParams are used by views to tell their parents how they want to be laid out. See
The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding)
an exact number
There are subclasses of LayoutParams for different subclasses of ViewGroup. For example, AbsoluteLayout has its own subclass of LayoutParams which adds an X and Y value.
全是英文。。。不想看,直接谷歌翻译的。意思就是子view告诉父view自己布局的意愿。为什么说是子view告诉父view自己的意愿呢,因为是主动设置的,而不是被动在父view中被父view设置的。所以是告诉父view自己的意愿。
设置宽、高的时候有三种参数:1.填充父view,2.包裹子view,3.确定的值。
1.DisplayMetrics
2.LayoutParams
一、DisplayMetrics类
A structure describing general information about a display, such as its size, density, and font scaling.To access the DisplayMetrics members, initialize an object like this
这个类是获取手机屏幕信息的一个类,例如尺寸、分辨率和字体尺寸(自己翻译的文档,不对请指正),初始化DisplayMetrics方法如下:
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
getWindowManager().getDefaultDisplay().getMetrics(metrics);此时metrics是有当前屏幕信息的。包括分辨率、长、宽、字体分辨率等信息。
二、LayoutParams类
继承自Android.View.ViewGroup.LayoutParamsLayoutParams are used by views to tell their parents how they want to be laid out. See
ViewGroup Layout Attributesfor a list of all child view attributes that this class supports.
The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding)
an exact number
There are subclasses of LayoutParams for different subclasses of ViewGroup. For example, AbsoluteLayout has its own subclass of LayoutParams which adds an X and Y value.
全是英文。。。不想看,直接谷歌翻译的。意思就是子view告诉父view自己布局的意愿。为什么说是子view告诉父view自己的意愿呢,因为是主动设置的,而不是被动在父view中被父view设置的。所以是告诉父view自己的意愿。
设置宽、高的时候有三种参数:1.填充父view,2.包裹子view,3.确定的值。
相关文章推荐
- Android实现表情 抓取新浪表情
- 详解Android解析Xml的三种方式——DOM、SAX以及XMLpull
- android开发环境遇到adt无法启动的问题分析及解决方法
- Android开发 旋转屏幕导致Activity重建解决方法
- Android开发技巧之在a标签或TextView控件中单击链接弹出Activity(自定义动作)
- Android开发技巧之ViewStub控件惰性装载
- Android开发常用经典代码段集锦
- 分享10个很棒的学习Android开发的网站
- android开发之横向滚动/竖向滚动的ListView(固定列头)
- Android开发技巧之我的菜单我做主(自定义菜单)
- Windows下使用Apache Cordova开发ANDROID之HelloWorld
- 根据根据图片的url怎么取得图片ImageView对象
- [转] ADB server didn't ACK
- android开发的相关链接
- Android数据存储之SharedPreferences 共享首选项/偏好设置
- 基于NDK的Android防破解
- 麦子学院android开发笔记:Android更换RatingBar图片
- android开发入门知识之AIDL详解
- AutoCompleteTextView组件的功能和用法