自定义view有三种情况,1.绘制view,2.组合控件,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 metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
getWindowManager().getDefaultDisplay().getMetrics(metrics);此时metrics是有当前屏幕信息的。包括分辨率、长、宽、字体分辨率等信息。
二、LayoutParams类
继承自Android.View.ViewGroup.LayoutParams
LayoutParams are used by views to tell their parents how they want to be laid out. See ViewGroup Layout Attributes
for 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
全是英文。。。不想看,直接谷歌翻译的。意思就是子view告诉父view自己布局的意愿。为什么说是子view告诉父view自己的意愿呢,因为是主动设置的,而不是被动在父view中被父view设置的。所以是告诉父view自己的意愿。
设置宽、高的时候有三种参数:1.填充父view,2.包裹子view,3.确定的值。