您的位置:首页 > 移动开发 > Android开发

自定义view中用到了两个类--获取屏幕信息、设置布局

2016-02-28 10:19 501 查看
自定义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方法如下:
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

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开发