1. 程式人生 > >自定義view中用到了兩個類--獲取螢幕資訊、設定佈局

自定義view中用到了兩個類--獲取螢幕資訊、設定佈局

 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 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.確定的值。