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

自定义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.确定的值。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值